0

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

Shehzad Bilal
  • 2,535
  • 2
  • 18
  • 27

3 Answers3

4

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];
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • @ShehzadBilal Glad to help. See link in Alladinian's answer for a better way to do this. My way just assumes that the camera session will be started within 1 second whereas his answer listens for a notification from the camera that is send when it is ready to take a picture. – Mick MacCallum Jul 18 '12 at 12:38
2

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.

Community
  • 1
  • 1
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • I beat you by a few seconds, but your answer is definitely better +1 – Mick MacCallum Jul 18 '12 at 12:35
  • @MDT Thanx man :) Just as a note to the 'delay` solution: Be careful because in some devices the session needs much more time to start. Apart from that, I guess that it's something that would work on older iOS versions – Alladinian Jul 18 '12 at 12:39
  • Thanks for the advice, yeah I wasn't thinking about it but that's a great point. – Mick MacCallum Jul 18 '12 at 12:40
0

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

Community
  • 1
  • 1
tmr
  • 1,500
  • 15
  • 22