以前的解决方案已被弃用 - 请不要下载或标记为重复
UIImagePickerControllerReferenceURL
在iOS 11中已被弃用(即使它仍然在info
字典中返回)并且应该被UIImagePickerControllerPHAsset
替换,但我还没有得到{ {1}}包含该密钥的字典。由于一个被弃用而另一个被丢弃,是否有一个已知的解决方案用于从所选图像中提取“采取日期”?
作为参考,这是从库中挑选图像时返回的示例info
字典:
info
(请注意▿ 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
可能是Apple忘记实施if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset,
let resource = PHAssetResource.assetResources(for: asset).first {
let dateTaken = resource.creationDate
}
了吗?关于变通方法的任何想法(不使用弃用的方法)?
答案 0 :(得分:3)
您可以通过ImageIO框架检查所选照片的元数据来获取日期。
然而,PHAsset信息无法到达的说法仅仅是虚假的。它到达就好了。你没有显示你的代码,所以谁知道你在做什么?也许问题是你忘了获得用户授权?如果没有课程的用户授权,您将无法访问PHAsset。你将获得UIImagePickerControllerOriginalImage和UIImagePickerControllerImageURL以及所有这些。
这段代码对我来说很合适(假设我们在使用图像选择器控制器之前拥有必要的用户授权):
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) { //
let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
let url = info[UIImagePickerControllerMediaURL] as? URL
var im = info[UIImagePickerControllerOriginalImage] as? UIImage
if let ed = info[UIImagePickerControllerEditedImage] as? UIImage {
im = ed
}
let live = info[UIImagePickerControllerLivePhoto] as? PHLivePhoto
let imurl = info[UIImagePickerControllerImageURL] as? URL
self.dismiss(animated:true) {
if let style = asset?.playbackStyle { // and so on, works fine
// ....
}
}
}
答案 1 :(得分:3)
Swift 4.1
我也挣扎了一段时间。事实证明,您只需要用户权限即可访问照片库 - 然后info
将包含密钥UIImagePickerControllerPHAsset
的值。你可以检查&amp;请求访问如下:
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