UIImagePickerController抛出错误

时间:2018-03-09 17:26:32

标签: ios swift uiimagepickercontroller

我有以下代码打开照片库或相机,但每当我运行代码时,我都会收到运行时错误:

WhatsUp[28458:10570522] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: _UIImagePickerControllerMediaTypes)'
*** First throw call stack:

这是我的代码:

 // open the camera
        self.imagePicker = UIImagePickerController(rootViewController: self)
        self.imagePicker.delegate = self

        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            self.imagePicker.sourceType = .camera

        } else {
            self.imagePicker.sourceType = .photoLibrary
        }

        self.present(self.imagePicker, animated: true, completion: nil)

我已将隐私密钥添加到info.plist文件中,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:5)

您在创建图像选择器时使用的是继承的UINavigationController初始值设定项。但正确的方法是使用没有参数的初始化器。

该行:

self.imagePicker = UIImagePickerController(rootViewController: self)

需要:

self.imagePicker = UIImagePickerController()

使用错误的初始化程序,图像选择器设置不正确。