How to retrieve PHAsset from UIImagePickerControll

2020-06-03 01:20发布

问题:

I'm trying to retrieve a PHAsset however PHAsset.fetchAssets(withALAssetURLs:options:) is deprecated from iOS 8 so how can I properly retrieve a PHAsset?

回答1:

I had the same the issue, first check permissions and request access:

let status = PHPhotoLibrary.authorizationStatus()

if status == .notDetermined  {
    PHPhotoLibrary.requestAuthorization({status in

    })
}

Just hook that up to whatever triggers your UIImagePickerController. The delegate call should now include the PHAsset in the userInfo.

guard let asset = info[UIImagePickerControllerPHAsset] as? PHAsset


回答2:

Here is my solution:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if #available(iOS 11.0, *) {
               let asset = info[UIImagePickerControllerPHAsset]

        } else {
               if let assetURL = info[UIImagePickerControllerReferenceURL] as? URL {
               let result = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil)
               let asset = result.firstObject
               }
       }
}


回答3:

The PHAsset will not appear in the didFinishPickingMediaWithInfo: info result unless the user has authorized, which did not happen for me just by presenting the picker. I added this in the Coordinator init():

            let status = PHPhotoLibrary.authorizationStatus()

            if status == .notDetermined  {
                PHPhotoLibrary.requestAuthorization({status in

                })
            }


回答4:

I am not sure what you want.

Are you trying to target iOS 8?

This is how I fetch photos and it works in iOS (8.0 and later), macOS (10.11 and later), tvOS (10.0 and later). Code is commented where it may be confusing

  1. The first functions sets the options to fetch the photos
  2. The second function will actually fetch them

     //import the Photos framework
     import Photos
     //in these arrays I store my images and assets
     var images = [UIImage]()
     var assets = [PHAsset]()
    
     fileprivate func setPhotoOptions() -> PHFetchOptions{
            let fetchOptions = PHFetchOptions()
            fetchOptions.fetchLimit = 15
            let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
            fetchOptions.sortDescriptors = [sortDescriptor]
            return fetchOptions
        }
    
    
    
    
    fileprivate func fetchPhotos() {
    let allPhotos = PHAsset.fetchAssets(with: .image, options: setPhotoOptions())
    
    DispatchQueue.global(qos: .background).async {
    
        allPhotos.enumerateObjects({ (asset, count, stop) in
            let imageManager = PHImageManager.default()
            let targetSize = CGSize(width: 200, height: 200)
            let options = PHImageRequestOptions()
            options.isSynchronous = true
            imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
    
                if let image = image {
                    self.images.append(image)
                    self.assets.append(asset)
    
    
                }
    
                if count == allPhotos.count - 1 {
                    DispatchQueue.main.async {
                        //basically, here you can do what you want
                        //(after you finish retrieving your assets)
                        //I am reloading my collection view
                        self.collectionView?.reloadData()
                    }
                }
    
            })
    
    
        })
    
    }
    

    }

Edit based on OP's clarification

You need to set the delegate UIImagePickerControllerDelegate

then implement the following function

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

within said method, get the image like this:

var image : UIImage = info[UIImagePickerControllerEditedImage] as! UIImage