Shehzad Bilal
Shehzad Bilal

Reputation: 2523

UIImagePickerController delegate is not calling

Iam using the following code for taking the picture automatically from IPAD front camera:

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

        imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
        [self presentModalViewController:imgPkr animated:YES];
        imgPkr.showsCameraControls = NO;
        [imgPkr takePicture];

But this code Dont take any picture and dont call the delegate:

    -(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info

Any idea what is wrong with the code

Upvotes: 0

Views: 2865

Answers (3)

tmr
tmr

Reputation: 1530

another way to wait for camera ready until take picture is completion block ->

[self presentViewController:imagePicker animated:YES
                 completion:^ {
                     [imagePicker takePicture];
                 }];

thank you 'GrandSteph' at iOS taking photo programmatically

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130193

My first guess is that [imgPkr takePicture]; is being called before the picker is done being presented. Try it like this:

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

        imgPkr.cameraDevice=UIImagePickerControllerCameraDeviceFront;
        [self presentModalViewController:imgPkr animated:YES];
        imgPkr.showsCameraControls = NO;
        [self performSelector:@selector(takePicture) withObject:self afterDelay:1.0];

OR

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(takePicture)
                                             name:AVCaptureSessionDidStartRunningNotification 

object:nil];

&

- (void)takePicture
{
    [imgPkr takePicture];
}

Upvotes: 4

Alladinian
Alladinian

Reputation: 35636

I bet that you get a message in your logs saying that

UIImagePickerController: ignoring request to take picture; camera is not yet ready.

This is a common problem because the underlying capture session needs some time to start, so you just have to be sure that the camera is ready to take a picture and then call takePicture method. Now, a way to get notified is explained in detail in my answer here: How to know if iPhone camera is ready to take picture? Note though that this method will work on iOS5+ (there is a bug in older versions that prevents the system notifications for this event, contrary to what is described in the documentation). I hope that this helps.

Upvotes: 2

Related Questions