How to get image name from UIImagePickerControllerPHAsset taken using library or camera?

I want to get the file name from UIImagePickerControllerPHAsset in ios 11.0.3. Failed to get the selected image file name using this code. please help with this?

if #available(iOS 11.0, *) { let asset = info[UIImagePickerControllerPHAsset] as? PHAsset fileName = ((asset?.value(forKey: "filename")) as? String)! } else { // Fallback on earlier versions let url = info[UIImagePickerControllerReferenceURL] as! URL let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil) fileName = PHAssetResource.assetResources(for: assets.firstObject!).first!.originalFilename } 

Getting crashes in the fileName = ((asset? .Value (forKey: "filename")) as? String)!

+11
source share
3 answers

Apple doesn't seem to have added the new iOS 11 key to the info dictionary. Therefore, the iOS 11 code will not work as it continues to return zero. See This Open Radar http://www.openradar.me/34404703

The only solution I have found so far is to use the iOS 10 code code even in iOS 11 and just ignore the deprecated UIImagePickerControllerReferenceURL message (the key still exists and works in iOS 11)

Hope this helps

+5
source

If you have access to a custom photo library, you can get the PHAsset value, as expected from UIImagePickerController . It seems that the UIImagePickerController will not request permission on your behalf, so you will have to do it yourself.

See Determine if access to the photo library, PHPhotoLibrary , is set up for how you can check and request permissions.

+1
source

You can try this ...

 if #available(iOS 11.0, *) { let asset = info[UIImagePickerControllerPHAsset] as! PHAsset var resources = PHAssetResource.assetResources(for: asset) var orgFilename = (resources[0]).originalFilename print("File Name: ",orgFilename) } 
+1
source

Source: https://habr.com/ru/post/1272911/


All Articles