Danail
Danail

Reputation: 10583

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;        
imagePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];

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

Upvotes: 19

Views: 14195

Answers (6)

JoséCaballero
JoséCaballero

Reputation: 1

if you're using UIImagePickerController on swiftUI View set your UIImagePickerController with @State.. Example:

import SwiftUI

struct SimpleView: View {
@State var cameraPicker:UIImagePickerController = UIImagePickerController()
    var body: some View {
         CameraView(picker:cameraPicker)
     }
}

Upvotes: -1

Wes
Wes

Reputation: 1069

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.

Upvotes: 7

Mehrdad
Mehrdad

Reputation: 1208

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 :)

Upvotes: 10

Ilker Baltaci
Ilker Baltaci

Reputation: 11779

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

Upvotes: 15

kalpesh jetani
kalpesh jetani

Reputation: 1803

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 
}

Upvotes: 3

nevan king
nevan king

Reputation: 113747

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

Upvotes: 39

Related Questions