Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to get the file name from UIImagePickerControllerPHAsset in ios 11.0.3. unable to get picked image file name using this code. please help on 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 on fileName = ((asset?.value(forKey: "filename")) as? String)!

like image 707
User558 Avatar asked Oct 26 '17 10:10

User558


Video Answer


1 Answers

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)
            }
like image 130
Mobi Avatar answered Nov 26 '22 01:11

Mobi