Camera Application to Take Pictures and Save Images to Photo Album
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); [picker release]; }
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 { UIAlertView *alert; // Unable to save the image if (error) alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to save image to Photo Album." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; else // All is well alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image saved to Photo Album." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; [alert release]; }
Optional Updates
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).
Source Code
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.








Is there a why to have it save to other dir after it does this
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// Also save to
/private/var/mobile/Library/Backup
Hi Mike,
You can call UIImageJPEGRepresentation() or UIImagePNGRepresentation() with a UIImage and then write the returned NSData to a file saving it in a location you specify.
Here is an example on one way to do this: Save UIImage Object as a PNG or JPEG File
John
Hi John,
This is really helpful to me. Is it possible to mail that image instead of saving into Photo album.
Please help.
Thanks
Hello Umesh,
Yes, that would be possible. I just published a new post showing an example here: How to Send Email with Attachments – Example Using iPhone Camera to Email a Photo
John
Great thank you i will bookmark this page.
thanx for the guide…
is there any way of giving the file a name, so when the user downloads the picture from the phone it will have a meaningful name? this is very important for me because i need to sync the pictures saved with other data.
Dylan
Dylan,
I don’t think there is a means to specify a filename when saving to the Photo Album. The alternative would be to save the image from the camera to a filename and location that you specify. I wrote a post showing how to do that here: Save UIImage Object as a PNG or JPEG File
Hi John,
I’m a little new to this so bear with me!
I’m trying to use the code above to open the camera when a button is clicked. Am I right in thinking that the first para of code goes into the .h file between the curly brackets, and the second 2 para’s go into the .m file to define the method once the button is clicked – in my case…
- (IBAction)takePhoto:(id)sender; {
“SECOND 2 PARAS IN HERE”
}
Thanks in advance
Alan
Hi Alan,
If you download the source code for the project you can see how I use the code shown above in a complete working example. That is probably a good starting point in which you can modify the code to see how things work.
Hope that helps.
John
Fantastic work – I’ve been struggling with the Apple docs on this topic and your code was great bootstrap for me.
Can you point me in the direction of a technique which will allow me to overlay or superimpose a frame or other “border” image while the camera is active and save the “merged” image?
Thanks in advance.
JMT
JMT, interesting idea, I’m not sure off hand how to overlay an image on the camera (or if it’s possible). Anyone have any ideas?
Where do you dealloc memory for the imagePicker? Do you find any leaks if you activate the camera multiple times?
Charles, you do need to release the imagePicker to keep from creating a leaked object.
Hi John,
This is what I am searching for. Thank you for this page.
Hi there.
Tried to run the app, but every time I build, I get the “The executable was signed with invalid entitlements.” error.
I am a paid developer, so something is wrong with the zipped file.
You will need to update the build settings to match your developer credentials.
Do you (or any of the commenters here) have a sample or tutorial that shows you how to make an app that just uses the LED on the iPhone 4? I want to create a flashlight app with strobing….
Thanks