5

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

3

7 Answers 7

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But it's not working in my case, asset always return nil.
Yes, now info doesn't provide "UIImagePickerControllerPHAsset" key.
you need to get photos permission before trying to access PhAsset info
How to get permission ? i have already added Privacy - Photo Library Usage Description
7

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

2 Comments

lastPathComponent will return you the temp file name set by system
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
6

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

2 Comments

This code is working fine, kindly try the code before downvoting.
Just tried, this does not work for photo taken by camera :).
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!

Comments

3

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
}

Comments

2

The correct way of doing is:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  var imageName = "image_1"
  if let asset = info[.phAsset] as? PHAsset, let fileName = asset.value(forKey: "filename") as? String {
    imageName = fileName
  } else if let url = info[.imageURL] as? URL {
    imageName = url.lastPathComponent
  }
}

PHAsset will be nil if the user hasn't given your app permissions to access library. This permission is no longer required if you use default UIImagePickerController, so if your app doesn't have them the OS will give you temporary access to a copy of the image, which all you can do is fall back to the path URL.

Comments

0

Please try this one.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.