I need to pick the image name from photo library . I am using UIImagePicker and I got image name , but its working only in simulators not in actual devices.
Asked
Active
Viewed 117 times
0
-
should be a lot of answers here in SO for this question like this one https://stackoverflow.com/questions/24625687/swift-uiimagepickercontroller-how-to-use-it – Reinier Melian Jan 08 '19 at 09:50
-
I tried many answers posted in stack overflow . But that's only working in simulators. – Jan 08 '19 at 10:07
-
Did you allow the use of Photos in your app? you need to add in info.plist file this key "Privacy - Camera Usage Description" with a message, let me know if solves your issue – Reinier Melian Jan 08 '19 at 10:10
-
Thats already added – Jan 08 '19 at 10:15
2 Answers
1
Please try this: In your UIImagePickerControllerDelegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
It may helps you. Thank you.

Sanjukta
- 1,057
- 6
- 16
-
I tried this code. But its only working in simulator .. In actual device getting null value. I don't know why – Jan 08 '19 at 10:05
-
-
I using iOS 12, but i don't see "UIImagePickerControllerPHAsset" in dic "info". – Son Pham Jan 08 '19 at 10:33
-
@AMP please Click the Up arrow of my answer so that another user get the help. – Sanjukta Jan 08 '19 at 11:09
0
Here is the perfectly working code for this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismissMe();
if let picImgURL = info[UIImagePickerControllerImageURL] as? URL {
let imageName = picImgURL.lastPathComponent
}
}

Mehul Thakkar
- 12,440
- 10
- 52
- 81