我有这段代码:
这里展示了UIImagePicker完美的效果。当我想要使用选中的项目获取PHAsset的日期时:
现在,当我选择一个视频时,例如
在其他地方(参见UIImagePickerControllerDelegate从选定的图像中获取日期在iOS 11中),我看到也许是因为我需要授权才能选择PHAssets。
所以我将我的第一段代码更改为:
然而现在当我按下导入按钮时,它会崩溃并显示lldb错误:
所以显然我没有做对。我应该怎么做呢?
@IBAction func importButtonPressed(_ sender: Any) {
self.imagePicker.sourceType = .photoLibrary
self.imagePicker.allowsEditing = true
self.imagePicker.mediaTypes = [kUTTypeMovie as String, kUTTypeImage as String]
self.present(self.imagePicker,animated: true, completion: nil)
}
这里展示了UIImagePicker完美的效果。当我想要使用选中的项目获取PHAsset的日期时:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard info[UIImagePickerControllerMediaType] != nil else { return }
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
switch mediaType {
case kUTTypeImage:
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
...
}
break
case kUTTypeMovie:
if let videoURL = info[UIImagePickerControllerMediaURL] as? URL, let pickedAsset = info[UIImagePickerControllerPHAsset] as? PHAsset {
print("kUTTypeMovie")
MyVariables.isScreenshot = false
let creationDate = pickedAsset.creationDate
print(creationDate,"creationDate")
}
break
case kUTTypeLivePhoto:
print("livePhoto")
dismiss(animated: true, completion: nil)
break
default:
dismiss(animated: true, completion: nil)
print("something else")
break
}
}
现在,当我选择一个视频时,例如
print(“kUTTypeMovie”)
失败,我认为这是因为let pickedAsset = info [UIImagePickerControllerPHAsset] as?PHAsset
失败。在其他地方(参见UIImagePickerControllerDelegate从选定的图像中获取日期在iOS 11中),我看到也许是因为我需要授权才能选择PHAssets。
所以我将我的第一段代码更改为:
@IBAction func importButtonPressed(_ sender: Any) {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized {
self.imagePicker.sourceType = .photoLibrary
self.imagePicker.allowsEditing = true
self.imagePicker.mediaTypes = [kUTTypeMovie as String, kUTTypeImage as String]
self.present(self.imagePicker,animated: true, completion: nil)
}
})
case .denied:
print("denied")
// probably alert the user that they need to grant photo access
case .notDetermined:
print("not determined")
case .restricted:
print("restricted")
// probably alert the user that photo access is restricted
}
}
然而现在当我按下导入按钮时,它会崩溃并显示lldb错误:
libsystem_kernel.dylib`__abort_with_payload:
0x1854f7040 <+0>: mov x16, #0x209
0x1854f7044 <+4>: svc #0x80
-> 0x1854f7048 <+8>: b.lo 0x1854f7060 ; <+32>
0x1854f704c <+12>: stp x29, x30, [sp, #-0x10]!
0x1854f7050 <+16>: mov x29, sp
0x1854f7054 <+20>: bl 0x1854d8bdc ; cerror_nocancel
0x1854f7058 <+24>: mov sp, x29
0x1854f705c <+28>: ldp x29, x30, [sp], #0x10
0x1854f7060 <+32>: ret
所以显然我没有做对。我应该怎么做呢?