简体   繁体   English

你如何从 UIImagePickerController 获取图像文件名?

[英]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.注意: PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)已弃用。

Swift only仅 Swift

Thanks谢谢

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.这将返回照片库中的实际文件名。

Do you mean the image file name?你的意思是图像文件名? If so, this should help you.如果是这样,这应该对您有所帮助。 In your UIImagePickerControllerDelegate:在您的 UIImagePickerControllerDelegate 中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
    print(fileUrl.lastPathComponent)
}

Update for iOS 12, Swift 4,针对 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)
}

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
}

UIImagePickerControllerPHAsset is nil if you don't have granted permissions to access the photo library.如果您没有授予访问照片库的权限,则UIImagePickerControllerPHAsset nil 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!操作系统不会自动询问,仍然会让您访问该文件(我不知道这背后的逻辑是什么!)但 PHAsset 是信息字典将为零!

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.如果用户未授予您的应用访问库的权限,则PHAsset将为nil 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.如果您使用默认的UIImagePickerController ,则不再需要此权限,因此如果您的应用程序没有它们,操作系统将为您提供对图像副本的临时访问权限,您所能做的就是回退到路径 URL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM