How should I get ALAssetURL by UIImagePickerController delegate?

267 views Asked by At

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) {
        //
    }
}
0

There are 0 answers