How do you get image file name from UIImagePickerController?
Solution 1
I found a way
if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}
This will return you the actual file name in the photo library.
Solution 2
Do you mean the image file name? If so, this should help you. In your UIImagePickerControllerDelegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
print(fileUrl.lastPathComponent)
}
Solution 3
Update for iOS 12, Swift 4,
This will give you the file name and file extension directly
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let fileUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL else { return }
print(fileUrl.lastPathComponent) // get file Name
print(fileUrl.pathExtension) // get file extension
dismiss(animated: true, completion: nil)
}
Solution 4
UIImagePickerControllerPHAsset
is nil
if you don't have granted permissions to access the photo library. The OS will not ask automatically and will still give you access to the file (I have no idea what's the logic behind this!) but the PHAsset is the info dictionary will be nil!
Solution 5
Try with below code
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
if let fileName = (asset.value(forKey: "filename")) as? String {
print(\(fileName))
}
}
// Other Stuff
}
Akshay Sood
Updated on October 09, 2022Comments
-
Akshay Sood over 1 year
I'm not getting the image name from photo library. Anyone here knows a proper way to get image name?
Note:
PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
this is deprecated.Swift only
Thanks
-
Sagar Chauhan over 6 yearsPlease see this: stackoverflow.com/questions/40627901/… May be help you.
-
Akshay Sood over 6 years@Jacky PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil) is deprecated
-
Moin Shirazi over 6 yearsPossible duplicate of How to obtain the original file name of the image picked by UIImagePickerController?
-
-
Akshay Sood over 6 yearslastPathComponent will return you the temp file name set by system
-
Akshay Sood over 6 yearscheck my answer. This will return you the exact file name. I have checked the file name on mac also and its returning you the correct file name
-
Sujit Baranwal almost 6 yearsBut it's not working in my case, asset always return nil.
-
jomafer over 5 yearsYes, now info doesn't provide "UIImagePickerControllerPHAsset" key.
-
Dot Freelancer about 5 yearsyou need to get photos permission before trying to access PhAsset info
-
Ankit about 5 yearsThis code is working fine, kindly try the code before downvoting.
-
godzilla over 4 yearsJust tried, this does not work for photo taken by camera :).
-
pankaj nigam almost 4 yearsHow to get permission ? i have already added Privacy - Photo Library Usage Description