选择时如何获取 uiCollectionView 原始资产或图像,以及如何将其传回上一个视图控制器并加载到 imageView

问题描述

我有一个集合视图可以在主 viewController 上提供一个 imageView。 我有三个问题:

1-我被迫在 collectionView 中使用“highQualityFormat”,因为我无法在 didSeleect 函数获取原始图像。有什么方法可以获取原始图像?

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       
     
     }

uicollection 视图:


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)

    }
}

解决方法

我找到了我自己问题的第二部分的解决方案。 我不得不将 fetchOption 用于 phAsset 而不是 phAssetCollection,这是我的错误。

_ = collection.count
        fetchOptions1.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)]
        photoAssets = PHAsset.fetchAssets(in: assetCollection,options: fetchOptions1) as! PHFetchResult<AnyObject>
,

嗨,我现在可以回答你的第三个问题,其他的我不确定。

您可以使用协议和委托将图像传递给前一个视图控制器。

您可以查看此Medium post