开发者

UIImagePickerController exception : "Source type must be UIImagePickerControllerSourceTypeCamera"

what is wrong with this? I really don't understand some important parts for UIImagePickerController....

here's the source:

     UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;        
im开发者_如何学编程agePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];

Can't I open the photo library? Any help appreciated!


If you're using the photo library, do you need to set cameraCaptureMode?


Maybe not in your case but when you initalize your UIImagePickerController object, FIRST you need to set your source type before setting any other properties otherwise you get the same error.

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


my problem was that i set camera device to .front before setting imagePicker.sourceType = .camera and obviously it caused crash. so i changed my code from :

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

to this :

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

and problem solved :)


I had this exact same exception, however I wasn't using an instance of UIImagePickerController control, instead I was showing a web page inside of a UIWebView that had an input w/ HTML Media Access attributes to enable camera access.

The problem is that it seems the UIWebView does not handle the HTML Media Access attributes properly. I noticed that the spec on these attributes changed a few times over time, so while it seemed like valid html and works on safari on the device, it doesn't work on pages rendered using UIWebView.

Bad:

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

Good:

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

The exception immediately went away and taking a picture works. This seems to be new to iOS 10 or 11 as it was existing code and didn't blow up in the past.


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 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜