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

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
  • Please see this: https://stackoverflow.com/questions/40627901/how-to-get-file-name-in-uiimagepickercontroller-with-asset-library May be help you. – Sagar Chauhan Nov 12 '17 at 07:12
  • 1
    @Jacky PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil) is deprecated – Akshay Sood Nov 12 '17 at 07:14
  • Possible duplicate of [How to obtain the original file name of the image picked by UIImagePickerController?](https://stackoverflow.com/questions/8242935/how-to-obtain-the-original-file-name-of-the-image-picked-by-uiimagepickercontrol) – Moin Shirazi Nov 12 '17 at 08:52

7 Answers7

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.

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
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)
}
Terje
  • 980
  • 9
  • 15
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)
}
Ankit
  • 638
  • 1
  • 8
  • 10
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
}
3

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!

AXE
  • 8,335
  • 6
  • 25
  • 32
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.

aramusss
  • 2,391
  • 20
  • 30
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)
} 
Kuldeep
  • 4,466
  • 8
  • 32
  • 59