0

When I use an ImagePicker by sourceType.camera, I want to get AssetUrl in order to get a PHAsset object. I called Info[UIImagePickerControllerReferenceURL], but it's always nil. Is there any way to resolve this? or some other ways replaced? I use Xcode 8.3 and Swift 3.1

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    //
    if info[UIImagePickerControllerReferenceURL] != nil{
        let imageAssetUrl: URL = (info[UIImagePickerControllerReferenceURL] as? URL)!
        print("\(imageAssetUrl)")

        let allPhotosOptions = PHFetchOptions()
        // sort by create
        allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
        //OriginalImage
        allPhotosOptions.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.image.rawValue)

        let fetchResult = PHAsset.fetchAssets(withALAssetURLs: [imageAssetUrl], options: allPhotosOptions)

        if fetchResult.count > 0 {

            let asset = fetchResult.firstObject

        }
    }

    picker.dismiss(animated: true) {
        //
    }
}
Y.Kay
  • 31
  • 3
  • Possible duplicate of [How do I get the data from UIImagePickerControllerReferenceURL?](http://stackoverflow.com/questions/10799605/how-do-i-get-the-data-from-uiimagepickercontrollerreferenceurl) – Oleg Gordiichuk Apr 24 '17 at 10:12
  • Please refer this one https://github.com/SanjeetVerma/UIImagePicker-PHAsset-swift – Bhupat Bheda Apr 24 '17 at 10:19

0 Answers0