UIImagePickerControllerDelegate didFinishPickingMediaWithInfo给我一个错误

时间:2018-12-13 23:00:38

标签: ios swift uiimagepickercontroller

我试图从设备上获取图像和视频,并试图声明UIImagePickerDelegateUINavigationControllerDelegate,但是在行guard let...上,这给了我一个错误:

  

不能用索引类型为“ UIImagePickerController.InfoKey”的下标“ [String:Any]”类型的值

在第let url = info...行,它给了我同样的错误。

我该如何处理?

//UIImagePickerControllerDelegate
extension SecondViewController: UIImagePickerControllerDelegate {
    private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        guard let mediaType = info[UIImagePickerControllerMediaType] as? String,
            mediaType == (kUTTypeMovie as String),
            let url = info[UIImagePickerControllerMediaURL] as? URL
            else { return }

        dismiss(animated: true) {
            let player = AVPlayer(url: url)
            let vcPlayer = AVPlayerViewController()
            vcPlayer.player = player
            self.present(vcPlayer, animated: true, completion: nil)
        }
    }
}

//UINavigationControllerDelegate
extension SecondViewController: UINavigationControllerDelegate {
}

1 个答案:

答案 0 :(得分:2)

使用最新语法

func imagePickerController(_ picker: UIImagePickerController, 
  didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

guard let mediaType = info[.mediaType] as? String,
    mediaType == (kUTTypeMovie as String),
    let url = info[.mediaURL] as? URL
    else { return }

    dismiss(animated: true) {
        let player = AVPlayer(url: url)
        let vcPlayer = AVPlayerViewController()
        vcPlayer.player = player
        self.present(vcPlayer, animated: true, completion: nil)
    }
 }
}