Camera Application to Take Pictures and Save Images to Photo Album

Fri, Feb 26

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.

32 comments

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

by mike on Mar 3, 2010. #

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

by John Muchow on Mar 3, 2010. #

Hi John,

This is really helpful to me. Is it possible to mail that image instead of saving into Photo album.

Please help.
Thanks

by Umesh jagtap on Mar 4, 2010. #

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

by John Muchow on Mar 4, 2010. #

Great thank you i will bookmark this page.

by mike on Mar 4, 2010. #

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

by Dylan on Mar 7, 2010. #

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

by John Muchow on Mar 7, 2010. #

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

by Alan G on Mar 20, 2010. #

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

by John Muchow on Mar 20, 2010. #

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

by JMT on Mar 29, 2010. #

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?

by John Muchow on Mar 29, 2010. #

Where do you dealloc memory for the imagePicker? Do you find any leaks if you activate the camera multiple times?

by Charles on May 6, 2010. #

Charles, you do need to release the imagePicker to keep from creating a leaked object.

by John Muchow on May 6, 2010. #

Hi John,

This is what I am searching for. Thank you for this page.

by Monish on Jun 28, 2010. #

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.

by alchemistmuffin on Jul 9, 2010. #

You will need to update the build settings to match your developer credentials.

by John Muchow on Jul 10, 2010. #

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

by Josh on Jul 11, 2010. #

It looks like there is some sort of setup that prevents the button click action being called in ‘viewDidLoad’ – at least, I did it, but it didn’t work. Is anyone aware of an application or view delegate method in which instantiating the UIImagePickerController would work?

by zenchemical on Aug 31, 2010. #

excellent..!!!

by rohit on Nov 9, 2010. #

John, no matter how much I appreciate your work it’ll never do justice to how great a job you are doing. Thank you.

by Arsalan on Dec 15, 2010. #

Great job, thanks for the info (I was stuck saving the images locally after invoking the camera, but not anymore) – thanks!

by bob on Jan 3, 2011. #

Thanks for the post on this. Have a problem though.

After the image is saved to the camera roll and the modalViewController is dismissed, the test_appViewController does not reappear with the button on it. Did the testing on the device, of course. Any clue as to why this is happening?

by James on Jan 10, 2011. #

thanx…

by raj on Sep 22, 2011. #

Thank You.. Good Work Mr. John

by Ali on Oct 12, 2011. #

Hi, great tutorial, Very simple and easy understanding. Few questions… I know this will NOT work in the simulator but tried anyways to see what would actually happen when i click the “Activate Camera” button. It crashes the app and i get the SIGABRT error.
Should it display this error since the camera will not run in the simulator?

Also how can i use the picture in my app instead of saving to photo gallery? Any documentation or reference much appreciated!

Thanks!
Steve

by Steven on Nov 9, 2011. #

Steve,

You will receive that error message, or something similar, when trying to access the camera in the simulator. You could put a check in there to see if the camera is available. Only allow camera access if it is available and then the app shouldn’t crash when you run it in your simulator.

Also, you can save to your application’s documents directory. You should be able to find a lot of documentation on this if you search for NSFileManager and use that in combination with returned array from the method call NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).

Charles

by Charles on Nov 9, 2011. #

How to save image from my current image. I have 10 pictures reside in the folder called pages/image1.jpg etc. My app is about ipad picturebook. When user swipe it goes to a new page. Below the page, i have a button to save a picture to photos album. How to do that ? Any tutorial for this ?

by Amin on Dec 10, 2011. #

Great tutorial,

I have integrated this into an app in which a have a view with the take picture button. When the button is pressed the camera is launched, I take the photo, and then save it to photo album. the message that it has been saved pops up and I click OK. Here is where the problem arrises. I am stuck in this view. How do I return to the view where I first pressed the take photo button? Thanks for all the help so far.

by Gary Benna on Feb 15, 2012. #

Typically you pop a view controller to return to the previous view, it depends how the view controller was shown. Look into the view controller documentation that Apple has, it describes all this in great detail.

by John Muchow on Feb 15, 2012. #

When I first read the article I took note of the original time stamp and almost left. I scrolled down to the comments and saw it had comments (with answers!) even two years down the road. Big thumbs up to the community and the author. RSS subscription time!

by Jake on Apr 23, 2012. #

Holy wowzers. I decide I want to subscribe so I scroll to the top. Figuring it would take 3 or four clicks to find what I needed. Nope… it’s right there at the top. I love you guys.

by Jake on Apr 23, 2012. #

Thnx Bade…
Nice tutorial…. simple and nice…. provide some more samples like this…

by Muhammed Ayaz on Feb 8, 2013. #