[英]How to get an authorized PHAsset from UIImagePickerController?
我有以下代碼:
@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
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
很明顯,我沒有正確地做到這一點。 我應該怎么做?
崩潰是由於以下缺少訪問照片庫的權限所致,
該應用已崩潰,因為它嘗試訪問沒有使用說明的隱私敏感數據。 該應用程序的Info.plist必須包含一個NSPhotoLibraryUsageDescription鍵,該鍵具有字符串值,該字符串值向用戶說明該應用程序如何使用此數據。
在您的Info.plist
,添加帶有以下描述的NSPhotoLibraryUsageDescription
鍵,它將正常工作
編輯您還應該按照以下適當方式使用PhotoLibrary
授權。
@IBAction func importButtonPressed(_ sender: Any) {
PHPhotoLibrary.requestAuthorization({status in
switch status {
case .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
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.