0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 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 Answers2

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
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