Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve PHAsset from UIImagePickerController

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?

like image 865
Peter Warbo Avatar asked Jul 07 '17 18:07

Peter Warbo


People also ask

What is Phasset in Swift?

A representation of an image, video, or Live Photo in the Photos library.

Is UIImagePickerController deprecated?

The photo library APIs from UIImagePickerController are deprecated.


2 Answers

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
like image 139
Andrew Geisthardt Avatar answered Sep 25 '22 08:09

Andrew Geisthardt


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
               }
       }
}
like image 39
Shahul Hasan Avatar answered Sep 25 '22 08:09

Shahul Hasan