UIImagePickerControllerDelegate 从 iOS 11 中选取的图像中获取日期 [英] UIImagePickerControllerDelegate get date from picked image in iOS 11

查看:29
本文介绍了UIImagePickerControllerDelegate 从 iOS 11 中选取的图像中获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIImagePickerControllerReferenceURL 在 iOS 11 中被弃用(即使它仍然在 info 字典中返回)并且应该被替换为 UIImagePickerControllerPHAsset,但我还没有得到一个包含该键的 info 字典.由于一个已弃用而另一个丢失,是否有已知的解决方案可以从所选图像中提取拍摄日期"?

UIImagePickerControllerReferenceURL was deprecated in iOS 11 (even though it's still returned in the info dictionary) and was supposed to be replaced with UIImagePickerControllerPHAsset, but I've yet to get an info dictionary back which contains that key. Since the one is deprecated and the other is missing, is there a known solution for extracting the "date taken" from the picked image?

作为参考,这是一个示例 info 从库中选取图像时返回的字典:

For reference, this is an example info dictionary returned when and image is picked from the library:

▿ 4 elements
  ▿ 0 : 2 elements
    - key : "UIImagePickerControllerImageURL"
    - value : file:///private/var/mobile/Containers/Data/Application/EE1BA60E-2DC3-47C5-A58D-86498E95C323/tmp/3A025D4C-B378-474B-8A09-017479A3A776.jpeg
  ▿ 1 : 2 elements
    - key : "UIImagePickerControllerMediaType"
    - value : public.image
  ▿ 2 : 2 elements
    - key : "UIImagePickerControllerReferenceURL"
    - value : assets-library://asset/asset.HEIC?id=537976CD-A550-41C9-9416-92C8072112D7&ext=HEIC
  ▿ 3 : 2 elements
    - key : "UIImagePickerControllerOriginalImage"
    - value : <UIImage: 0x1d04b4760> size {3024, 4032} orientation 3 scale 1.000000

(请注意,UIImagePickerControllerReferenceURL 仍然存在,但已弃用,并且缺少建议的替代品 UIImagePickerControllerPHAsset.)

(Note that UIImagePickerControllerReferenceURL is still present, though deprecated, and the suggested replacement, UIImagePickerControllerPHAsset, is missing.)

如果它存在,获取日期将很简单:

if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset,
    let resource = PHAssetResource.assetResources(for: asset).first {
    let dateTaken = resource.creationDate
}

会不会是苹果忘记实现UIImagePickerControllerPHAsset?关于变通方法的任何想法(不使用已弃用的方法)?

Could it be that Apple forgot to implement UIImagePickerControllerPHAsset? Any ideas on workarounds (without using deprecated methods)?

我相信以前关于 Stack Overflow 的解决方案已被弃用,因此不会使用现代方法回答这个问题.

I believe that previous solutions on Stack Overflow are deprecated, and thus won't answer the question using modern approaches.

推荐答案

Swift 4.1

我也为此挣扎了一段时间.事实证明,您只需要用户权限即可访问照片库 - 然后 info 将包含键 UIImagePickerControllerPHAsset 的值.你可以检查 &像这样请求访问:

I struggled with this for a while too. It turns out you just need user permission to access to the photo library - then info will contain a value for the key UIImagePickerControllerPHAsset. You can check & request access like this:

let status = PHPhotoLibrary.authorizationStatus()

switch status {
    case .authorized:
    // show your media picker
    case .denied:
    // probably alert the user that they need to grant photo access
    case .notDetermined:
    PHPhotoLibrary.requestAuthorization({status in
        if status == .authorized {
            // show your media picker
        }
    })
    case .restricted:
    // probably alert the user that photo access is restricted
}

然后你像往常一样使用 imagePickerController(_: didFinishPickingMediaWithInfo:) 方法,并且可以访问 UIImagePickerControllerPHAsset

Then you use the imagePickerController(_: didFinishPickingMediaWithInfo:) method as usual and will have access to the UIImagePickerControllerPHAsset

这篇关于UIImagePickerControllerDelegate 从 iOS 11 中选取的图像中获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆