Aks*_*ood 4 iphone uiimagepickercontroller ios swift
我没有从照片库中获取图像名称.这里的任何人都知道获取图像名称的正确方法吗?
注意:PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)这是不推荐使用的.
只有斯威夫特
谢谢
我找到了一个方法
if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}
Run Code Online (Sandbox Code Playgroud)
这将返回照片库中的实际文件名.
您的意思是图像文件名吗?如果是这样,这应该对您有帮助。在您的UIImagePickerControllerDelegate中:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
print(fileUrl.lastPathComponent)
}
Run Code Online (Sandbox Code Playgroud)
iOS 12、Swift 4 更新,
这将直接为您提供文件名和文件扩展名
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)
}
Run Code Online (Sandbox Code Playgroud)