Akshay Sood
Akshay Sood

Reputation: 6786

How do you get image file name from UIImagePickerController?

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

Upvotes: 5

Views: 21731

Answers (7)

aramusss
aramusss

Reputation: 2401

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.

Upvotes: 2

Ankit
Ankit

Reputation: 648

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

Upvotes: 6

user8324856
user8324856

Reputation:

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

Upvotes: 0

AXE
AXE

Reputation: 8465

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!

Upvotes: 4

Riddhi Jarsaniya
Riddhi Jarsaniya

Reputation: 31

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
}

Upvotes: 3

Terje
Terje

Reputation: 1000

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

Upvotes: 7

Akshay Sood
Akshay Sood

Reputation: 6786

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.

Upvotes: 8

Related Questions