- Rashid Khaleefa
Blog on how to integrate camera into iPhone application.
You can easily include camera feature into any ios application provided the device has a camera support, and you make use of the imagePickerController of apple.
You have to create a secondary view controller for managing the overlap view to the camera.
This customises the way how the camera screen should apper once camera is initiated from your application.
Use the apples imagepickerController to pic the image snaped.
- Declare the needed controlls in .h file
- Implement those in .m
- Include the UIImagePickerControllerDelegate
- Create needed delegates for your overlayViewController
A Sample overlay having buttons takepicture button and done button with actions takePhoto, close respectively for those. Also the delegate methods didTakePicture, the method where you get the snaped picture and didFinshWithCamera that can be used to perform any action once you are done with camera.
The .h file of overlayViewController:
@interface OverlayViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
id <OverlayViewControllerDelegate> delegate;
@property (nonatomic, assign) id <OverlayViewControllerDelegate> delegate;
@property (nonatomic, retain) UIImagePickerController *imagePickerController;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *takePictureButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *cancelButton;
// camera page (overlay view)
- (void)didTakePicture:(UIImage *)picture;
Now in you .m file, Initiate the imagePickerController and map its delegate
self.imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
self.imagePickerController.delegate = self;
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
// user wants to use the camera interface
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] == 0)
// setup your custom overlay view for the camera
// ensure that your custom view's frame fits within the parent frame
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0,
self.view.frame.size.height - 10.0,
self.view.frame.size.height + 10.0);
self.view.frame = newFrame;
This calls the didFinishWithCamera delegats.
Action that maps takesPhoto. This just calls the takePicture of imgaePickerController which is responsible for initiating still image capture.
Also include the follwoing delegates of imgePicker which gets called when an image is taken by the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self.delegate didFinishWithCamera]; // tell your delegate we are finished with the picker
Thats all for overlayViewController
Now How to use this in your class. You have to now include the overlayController delegate and imgaPickerController in header of the class where you wish to inclue a button for a camera capture.
Now call the below function in the button action you need.
NSLog(@"entered showImagePicker camera");
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
This checks for the sourcetype you give that should be a camera sourcetype the syntax is as below
Thats it now include the delegates of overlay class that was created
- (void)didTakePicture:(UIImage *)picture
//do some thing with the picture.
The didTakePicture tells that a picture was taken, you get the picture you snapped in this delegate. And didFinishWithCamera tells to finish with the camera.
Thats it. You get your camera to snap from your application and the image you snapped.