UIImagePickerController 抛出错误

UIImagePickerController is throwing error

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

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 文件中,如下所示:

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

行:

self.imagePicker = UIImagePickerController(rootViewController: self)

需要:

self.imagePicker = UIImagePickerController()

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