Home:ALL Converter>Capture picture automatically in IOS

Capture picture automatically in IOS

Ask Time:2013-10-22T05:31:18         Author:sim

Json Formatter

My requirement is to write a sample IOS app that would automatically capture a camera picture. Using the various S.O links provided I did implement the below code -

My CameraViewController.h class is defined as follows :

@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *ImageView;


And CameraViewController.m has the below code :

    NSLog(@"Setting the background now");

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    picker.showsCameraControls = NO;
    picker.navigationBarHidden = NO;
    picker.toolbarHidden = NO;
    [self presentViewController:picker animated:YES completion:NULL];

    NSLog(@"Taking the picture now");
   [picker takePicture];


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

    NSLog(@"Entered the case of finishing pictures");

- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker
    NSLog(@"Entered the case of cancel");


What the above code does is successfully launch the camera app , however I am not sure if the takePicture API is able to successfully click a picture . I do not see any saved pictures in the Photos app inside my Ipad so I assume that the picture has not been clicked . Can someone please tell me if my code above is correct or what do I need to do to automate the part of clicking the capture button once the Camera controls are displayed

Author:sim,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19505135/capture-picture-automatically-in-ios
GoZoner :

[Please go to 'Using UIImagePickerController to Select Pictures and Take Photos' in the Apple documentation for the property cameraOverlayView of class UIImagePickerController for a complete example application that does what you need, and more.]\n\nYou specified your CameraViewController as adopting the UIImagePickerControllerDelegate protocol and thus you must implement two messages:\n\n- (void) imagePickerController: (UIImagePickerController *) picker \n didFinishPickingMediaWithInfo: (NSDictionary *) info;\n\n\nand \n\n- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker;\n\n\nAs the iOS documentation describes, the NSDictionary* info has a key UIImagePickerControllerOriginalImage which will return the UIImage. Access it as something like:\n\nUIImage *snapshot = (UIImage *) [info objectForKey: UIImagePickerControllerOriginalImage];\n\n\nSince your plan is to take a picture automatically (w/o user interaction) using takePicture then be sure to specify\n\n picker.showsCameraControls = NO;\n",
David Wong :

You need to implement the UIImagePIckerControllerDelegate's imagePickerController:didFinishPickingMediaWithInfo: method.\n\nAfter that, look inside the mediaInfo dictionary and there's a UIImage inside it you can use.",
jabelch :

I know this is old, but a better alternative to using a timer (see the comments from the accepted answer) would be to implement the completion handler instead of passing in NULL.\n\n[self presentViewController:picker animated:YES completion:^{\n NSLog(@\"Taking the picture now\");\n [picker takePicture];\n}];\n\n\nThat way, the picture is taken consistently every time, and you don't waste time adding an unnecessary delay.",