UIImagePickerController异常:“源类型必须是UIImagePickerControllerSourceTypeCamera”

时间:2010-09-11 12:57:23

标签: iphone uiimagepickercontroller

这有什么问题?我真的不明白UIImagePickerController的一些重要部分....

这是来源:

     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;        
imagePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];

我不能打开照片库吗? 任何帮助表示赞赏!

5 个答案:

答案 0 :(得分:29)

如果您正在使用照片库,是否需要设置cameraCaptureMode

答案 1 :(得分:7)

也许不是你的情况,但是当你初始化你的UIImagePickerController对象时,首先你需要在设置任何其他属性之前设置你的源类型,否则你会得到同样的错误。

let imagePickerController =  UIImagePickerController()
imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
imagePickerController.cameraCaptureMode =   
UIImagePickerControllerCameraCaptureMode.Photo
imagePickerController.cameraDevice = .Front
imagePickerController.showsCameraControls = true;
imagePickerController.navigationBarHidden = false;
imagePickerController.toolbarHidden = false;
imagePickerController.delegate = self

答案 2 :(得分:4)

我的问题是我在设置.front之前将相机设备设置为imagePicker.sourceType = .camera,显然它导致了崩溃。 所以我改变了我的代码:

 imagePicker.cameraDevice = .front
 imagePicker.sourceType = .camera

到此:

imagePicker.sourceType = .camera
imagePicker.cameraDevice = .front

问题解决了:)

答案 3 :(得分:3)

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {

            imagePickerController.cameraDevice =  UIImagePickerControllerCameraDeviceFront;
        } else {
            imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        }

    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}
else {
    // do stuff ///....Alert 
}

答案 4 :(得分:1)

我有完全相同的异常,但是我没有使用UIImagePickerController控件的实例,而是在UIWebView中显示一个网页,该网页具有输入w / HTML Media Access属性以启用摄像头访问。

问题是UIWebView似乎无法正确处理HTML Media Access属性。我注意到这些属性的规格随着时间的推移发生了几次变化,所以虽然它看起来像有效的html并且在设备上的safari上工作,但它不适用于使用UIWebView渲染的页面。

为:

<input type="file" accept="image/*" capture="camera" />

好:

<input type="file" accept="image/*;capture=camera" />

这个例外立即消失并拍照。这似乎是iOS 10或11的新功能,因为它是现有代码,并且在过去没有爆炸。