你如何从 UIImagePickerController 获取图像文件名?
How do you get image file name from UIImagePickerController?
我没有从照片库中获取图像名称。
这里有人知道获取图像名称的正确方法吗?
注意:PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
这已被弃用。
Swift 仅
谢谢
我找到方法了
if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}
这将 return 您照片库中的实际文件名。
你说的是图片文件名吗?如果是这样,这应该对您有所帮助。在你的 UIImagePickerControllerDelegate 中:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
print(fileUrl.lastPathComponent)
}
试试下面的代码
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
就是 nil
。 OS 不会自动询问并且仍然会让您访问该文件(我不知道这背后的逻辑是什么!)但是 PHAsset 是信息字典将为零!
请试试这个。
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)
}
更新 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)
}
正确的做法是:
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
如果用户未授予您的应用访问库的权限,则 nil
将是 nil
。如果您使用默认 UIImagePickerController
,则不再需要此权限,因此,如果您的应用没有它们,OS 将使您临时访问图像的副本,您所能做的就是 fall回到路径 URL.
我没有从照片库中获取图像名称。 这里有人知道获取图像名称的正确方法吗?
注意:PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
这已被弃用。
Swift 仅
谢谢
我找到方法了
if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}
这将 return 您照片库中的实际文件名。
你说的是图片文件名吗?如果是这样,这应该对您有所帮助。在你的 UIImagePickerControllerDelegate 中:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
print(fileUrl.lastPathComponent)
}
试试下面的代码
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
就是 nil
。 OS 不会自动询问并且仍然会让您访问该文件(我不知道这背后的逻辑是什么!)但是 PHAsset 是信息字典将为零!
请试试这个。
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)
}
更新 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)
}
正确的做法是:
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
如果用户未授予您的应用访问库的权限,则 nil
将是 nil
。如果您使用默认 UIImagePickerController
,则不再需要此权限,因此,如果您的应用没有它们,OS 将使您临时访问图像的副本,您所能做的就是 fall回到路径 URL.