How do you get image file name from UIImagePickerController?

18,779

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
}
Share:
18,779
Akshay Sood
Author by

Akshay Sood

Updated on October 09, 2022

Comments

  • Akshay Sood
    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

  • Akshay Sood
    Akshay Sood over 6 years
    lastPathComponent will return you the temp file name set by system
  • Akshay Sood
    Akshay Sood over 6 years
    check 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
    Sujit Baranwal almost 6 years
    But it's not working in my case, asset always return nil.
  • jomafer
    jomafer over 5 years
    Yes, now info doesn't provide "UIImagePickerControllerPHAsset" key.
  • Dot Freelancer
    Dot Freelancer about 5 years
    you need to get photos permission before trying to access PhAsset info
  • Ankit
    Ankit about 5 years
    This code is working fine, kindly try the code before downvoting.
  • godzilla
    godzilla over 4 years
    Just tried, this does not work for photo taken by camera :).
  • pankaj nigam
    pankaj nigam almost 4 years
    How to get permission ? i have already added Privacy - Photo Library Usage Description