我有一个集合视图来在主 viewController 上提供一个 imageView。我有三个问题:
1-我被迫在collectionView中使用“highQualityFormat”,因为我无法在didSelect函数上获取原始图像。有没有办法获取原始图像?
2- 在 phAssetCollection 中,我们不能将 sortDescriptors 与 creationDate 一起使用。如何对图像进行排序?startDate & endDate 无效。
3-如何在第一个 uiviewController 中加载选定的图像,而不是使用 userManager.shared 和 notificationCenter。感谢您的关注。
主视图控制器:
@IBOutlet weak var frontpic: UIImageView!
@IBAction func openPictureDialog(_ sender: Any) {
if !isLoadSoNew(){return}
selectedPic = 1
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "collectionVc1")//
present(vc,animated: true,completion: nil)
}
@objc func fillFront(_ notification:Notification){
frontpic.contentMode = .scaleAspectFit
frontpic.image = UserManager.shared.collectionViewImage
}
uicollectionView:
import UIKit
import Photos
class collectionVewControl: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var collectionView1: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
collectionView1.delegate = self
collectionView1.dataSource = self
fetchCustomAlbumPhotos()
collectionView1.frame.size.height = view.frame.height-60
}
var imageArray = [UIImage]()
var photo: UIImage? @IBAction func backButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
UserManager.shared.collectionViewImage = imageArray[indexPath.row]
NotificationCenter.default.post(name: Notification.Name(rawValue: "fillFront"), object: nil)
self.dismiss(animated: true, completion: nil)
}
func fetchCustomAlbumPhotos()
{
var albumName = "APKSoft"
var assetCollection = PHAssetCollection()
var photoAssets = PHFetchResult<AnyObject>()
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
// fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
if let firstObject = collection.firstObject{
//found the album
assetCollection = firstObject
}
else {
albumName = "Recents"
if let firstObject = collection.firstObject{
assetCollection = firstObject
}
}
_ = collection.count
photoAssets = PHAsset.fetchAssets(in: assetCollection, options: nil) as! PHFetchResult<AnyObject>
let imageManager = PHCachingImageManager()
photoAssets.enumerateObjects{(object: AnyObject!,
count: Int,
stop: UnsafeMutablePointer<ObjCBool>) in
if object is PHAsset{
let asset = object as! PHAsset
// print("Inside If object is PHAsset, This is number 1")
let imageSize = CGSize(width: asset.pixelWidth,
height: asset.pixelHeight)
/* For faster performance, and maybe degraded image */
let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.isSynchronous = true
imageManager.requestImage(for: asset,
targetSize: imageSize,
contentMode: .aspectFill,
options: options,
resultHandler: {
(image, info) -> Void in
self.photo = image!
/* The image is now available to us */
self.addImgToArray(uploadImage: self.photo!)
// print("enum for image, This is number 2")
})
}
}
}
func addImgToArray(uploadImage:UIImage)
{
self.imageArray.append(uploadImage)
}
}