How to retrieve PHAsset from UIImagePickerController

徘徊边缘 提交于 2020-01-12 04:45:30

问题


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:


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


来源:https://stackoverflow.com/questions/44977292/how-to-retrieve-phasset-from-uiimagepickercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!