Editor’s Note:If you would like to save the UIImage from the camera to a PNG of JPEG file versus the Photo Album, you can find an example here: Save UIImage Object as a PNG or JPEG File. Also, to email an image from the camera, take a look at this example: How to Send Email with Attachments – Example Using iPhone Camera to Email a Photo
It’s surprising easy to create a bare bones camera application on the iPhone. UIImagePickerController provides a means to access the camera, take a photo and preview the results. There is also an option to allow resizing and scaling of a photo once captured. Using UIImageWriteToSavedPhotosAlbum in the UIKit, you can easily save an image to the Photo Album.
The image on the left in the figure below shows the camera active in the application. The image on the right is the preview option once a photo has been taken.
Start the Camera
To work with the camera we begin by creating a UIImagePickerController object and setting the sourceType to UIImagePickerControllerSourceTypeCamera. For this example, I set allowsImageEditing to NO to disable editing of photos image. I use presentModalViewController to initiate the display of the camera.
// Create image picker controller
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Set source to the camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Delegate is self
imagePicker.delegate = self;
// Allow editing of image ?
imagePicker.allowsImageEditing = NO;
// Show image picker
[self presentModalViewController:imagePicker animated:YES];
Save Image to Photo Album
Once a photo has been taken, the method didFinishPickingMediaWithInfo will be called, providing the opportunity to write the image to the Photo Album:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
Notice the call selector reference above, this selector will be called once the image has been written to the system. For this example I display an alert showing the result of attempting to save the image:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
// Unable to save the image
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
This is a good, if not simple, place to start. Here are two additional updates to this application you can tinker with:
#1 – Check for a camera on device. Refer to this post Does iPhone Support Camera to learn more.
#2 – Set the image editing option to YES. This will require a few additional changes including a request to the information dictionary to return the cropping rectangle that was applied to the original image (see UIImagePickerControllerCropRect).
You can download the source code for the camera application here.
Simulator Not Supported
One last note, this application will only run on an actual device as the simulator does not have camera support.