iOS Developer Tips - Visitor Stats:
|
|
|
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
[Reply]
John Muchow Reply:
March 3rd, 2010 at 4:48 pm
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
[Reply]
Hi John,
This is really helpful to me. Is it possible to mail that image instead of saving into Photo album.
Please help.
Thanks
[Reply]
John Muchow Reply:
March 4th, 2010 at 8:25 am
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
[Reply]
Great thank you i will bookmark this page.
[Reply]
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
[Reply]
John Muchow Reply:
March 7th, 2010 at 9:12 am
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
[Reply]
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
[Reply]
John Muchow Reply:
March 20th, 2010 at 8:38 am
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
[Reply]
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
[Reply]
John Muchow Reply:
March 29th, 2010 at 9:02 pm
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?
[Reply]
Where do you dealloc memory for the imagePicker? Do you find any leaks if you activate the camera multiple times?
[Reply]
John Muchow Reply:
May 6th, 2010 at 9:07 pm
Charles, you do need to release the imagePicker to keep from creating a leaked object.
[Reply]
Hi John,
This is what I am searching for. Thank you for this page.
[Reply]
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.
[Reply]
John Muchow Reply:
July 10th, 2010 at 9:30 am
You will need to update the build settings to match your developer credentials.
[Reply]
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
[Reply]
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?
[Reply]
excellent..!!!
[Reply]
John, no matter how much I appreciate your work it’ll never do justice to how great a job you are doing. Thank you.
[Reply]
Great job, thanks for the info (I was stuck saving the images locally after invoking the camera, but not anymore) – thanks!
[Reply]
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?
[Reply]
thanx…
[Reply]
Thank You.. Good Work Mr. John
[Reply]
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
[Reply]
Charles Reply:
November 9th, 2011 at 9:27 am
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
[Reply]
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 ?
[Reply]