如何从UIImagePickerController获取授权的PHAsset?

时间:2018-06-03 19:19:07

标签: ios swift uiimagepickercontroller phasset

我有这段代码:

@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 get date from picked image in 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    

很明显,我没有正确地做到这一点。我该怎么做?

1 个答案:

答案 0 :(得分:3)

崩溃是因为以下缺少访问照片库的权限,

  

此应用已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据。应用程序的Info.plist必须包含一个NSPhotoLibraryUsageDescription键,其中包含一个字符串值,向用户解释应用程序如何使用此数据。

Info.plist中,添加NSPhotoLibraryUsageDescription密钥,其中包含如下所示的一些说明,它可以正常使用

enter image description here

修改您还应该以正确的方式使用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
        }
    })
}