简体   繁体   English

从 photoLibrary 获取图像的元数据

[英]Getting metadata of image from photoLibrary

I need to get the metaData from an Image I'm picking via UIImagePickerController.我需要从通过 UIImagePickerController 挑选的图像中获取元数据。 This is my code:这是我的代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        let metaData = info[UIImagePickerController.InfoKey.mediaMetadata] as? [AnyHashable: Any]
        print(metaData)
        picker.dismiss(animated: true, completion: nil)
    }

It works fine, when im picking the Image having.camera as source.当我选择具有.camera 的图像作为源时,它工作正常。 But when I use.photoLibrary as source, then metaData is nil.但是当我使用 .photoLibrary 作为源时,metaData 为零。 I already read through other questions and tried stuff like我已经阅读了其他问题并尝试了类似的东西

 let asset = info[.phAsset] as? PHAsset
 print(asset?.creationDate ?? "None")
 print(asset?.location ?? "None")

But this also returns nil.但这也返回 nil。 I guess the source of the problem is, that the info-Dictionary only returns 4 Keys when picking from.photoLibrary:我猜问题的根源是,从.photoLibrary 中挑选时,info-Dictionary 只返回 4 个键:

UIImagePickerControllerOriginalImage
UIImagePickerControllerMediaType
UIImagePickerControllerImageURL
UIImagePickerControllerReferenceURL

It would be awesome if someone could tell me where my mistake is.如果有人能告诉我我的错误在哪里,那就太棒了。 Thanks in advance !提前致谢 !

I had the same problem.我有同样的问题。 If the user does not grant access to the photo library first, info[.phAsset] will return nil.如果用户没有首先授予对照片库的访问权限,info[.phAsset] 将返回 nil。

Quote from Apple: "Accessing the photo library always requires explicit permission from the user. The first time your app uses PHAsset, PHCollection, PHAssetCollection, or PHCollectionList methods to fetch content from the library..." Apple 引用:“访问照片库始终需要用户的明确许可。您的应用第一次使用 PHAsset、PHCollection、PHAssetCollection 或 PHCollectionList 方法从库中获取内容时......”

Thus, you have to call PHPhotoLibrary.requestAuthorization{... } before presenting the image picker.因此,您必须在显示图像选择器之前调用 PHPhotoLibrary.requestAuthorization{... }。

If the user denies the request, info[.phAsset] will also be nil!如果用户拒绝请求,info[.phAsset] 也将为零!

This key is valid only when using an image picker whose source type is set to UIImagePickerController.SourceType.camera , and applies only to still images.此键仅在使用源类型设置为UIImagePickerController.SourceType.camera的图像选择器时有效,并且仅适用于静止图像。 The value for this key is an NSDictionary object that contains the metadata of the photo that was just captured.此键的值是一个NSDictionary object,其中包含刚刚捕获的照片的元数据。 To store the metadata along with the image in the Camera Roll, use the PHAssetChangeRequest class from the Photos framework.要将元数据与图像一起存储在相机胶卷中,请使用 Photos 框架中的PHAssetChangeRequest class。 https://developer.apple.com/documentation/uikit/uiimagepickercontroller/infokey/1619147-mediametadata https://developer.apple.com/documentation/uikit/uiimagepickercontroller/infokey/1619147-mediametadata

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

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