Picking Images with the iPhone SDK UIImagePickerController
March 19th, 2008 Posted in Software Development, iPhoneThe UIImagePickerController is the class you use when you want to import a picture from the iPhone photo library into your application. You can also use this class to open an interface that will allow you to take a picture and import that picture into your application.
The very simple application I’m going to describe opens a UIImagePickerController at startup. If the Cancel button is pressed on the image picker view the application will close. If a picture is selected the picture will be displayed full screen.
The main.m files is standard and just sets up the PickImageAppDelegate class as the application delegate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // from PickImageAppDelegate.h @interface PickImageAppDelegate : NSObject <UIApplicationDelegate, UIImagePickerControllerDelegate> { UIWindow* window; UIImagePickerController* imagePickerController; UIImageView* imageView; } @property (nonatomic, retain) UIWindow *window; - (void)applicationDidFinishLaunching:(UIApplication *)application; - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo; - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; @end |
The PickImageAppDelegate class implements the UIImagePickerControllerDelegate protocol. This allows it to receive imagePickerController:picker:didFinishPickingImage:editingInfo and imagePickerControllerDidCancel:picker messages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // from PickImageAppDelegate.m - (void)applicationDidFinishLaunching:(UIApplication *)application { // Create window self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Set up the image picker controller and add it to the view imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [window addSubview:imagePickerController.view]; // Set up the image view and add it to the view but make it hidden imageView = [[UIImageView alloc] initWithFrame:[window bounds]]; imageView.hidden = YES; [window addSubview:imageView]; [window makeKeyAndVisible]; } |
The UIImagePickerController object is created and its delegate is set the our PickImageAppDelegate instance. The sourceType is set to UIImagePickerControllerSourceTypePhotoLibrary which causes the picker to allow photos to be picked from the photo library. The other option for sourceType is UIImagePickerControllerSourceTypeCamera which allow you to take a new picture with the camera. The controller contains its own view and can be referenced from its view property so that it can be added as a sub view to the window.
1 2 3 4 5 6 7 8 9 10 11 12 13 | // from PickImageAppDelegate.m - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { // Dismiss the image selection, hide the picker and //show the image view with the picked image [picker dismissModalViewControllerAnimated:YES]; imagePickerController.view.hidden = YES; imageView.image = image; imageView.hidden = NO; [window bringSubviewToFront:imageView]; } |
The imagePickerController:picker:didFinishPickingImage:editingInfo method is called when an image is selected. The model selection dialog box is dismissed. The picker is hidden. The image view image is set to the selected image and then unhidden.
1 2 3 4 5 6 7 | // from PickImageAppDelegate.m - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { // Dismiss the image selection and close the program [picker dismissModalViewControllerAnimated:YES]; exit(0); } |
The imagePickerControllerDidCancel:picker method is called if the Cancel button is pressed on the picker view. The model selection dialog box is dismissed and the C exit() function is called to close the application.
As you can see it is very simple to select images from the photo library or take pictures with the built in camera and use them in your application.

![[del.icio.us]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/digg.png)
![[Reddit]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/reddit.png)
![[Technorati]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/technorati.png)
Related Articles:

This site looks promising for info on the iPhone SDK! I’m going to have to look at this abit closer!
Thanks for stopping by Orville!
Thank you for the TrailsInTheSand PickImage example project for the iPhone. It is very impressive.
However, Aspen iPhone Simulator v1.0 Beta (40) crashes (quits unexpectedly) when I try to use UIImagePickerControllerSourceTypeCamera instead of UIImagePickerControllerSourceTypePhotoLibrary. Have you had any success taking a photo through the simulator, presumably through the built-in camera in my MacBook Pro. I am using the recent iPhone SDK update 9a2151.
In the debugger, the simulator fails on the following line due to __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__:
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- Thanks, David Manpearl
Unfortunately the simulator doesn’t simulate the camera. It seems like it would be easy for Apple to just pass through a camera from the computer like the built in iSight cameras.
You can check to see if the camera is available by using this call:
BOOL camAvail = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
It will return false for the simulator.
[...] the Picking Images with the iPhone SDK UIImagePickerController article I gave some sample code using the UIImagePickerController to get images from your photo [...]
Great article!
Do you know how to scroll through the photoalbum?
How can you flick to the next image?
thanks,
Great post!
just a question, my simulator Photo DB is empty…
how can I put images on it ?
thanks
hi Mauro
i had the same problem, it comes from the fact that (i think) that you have chosen the “reset content and settings” option from the iPhone Simulator menu in the simulator. i had this problem and found that if you mount the SDK disk image, then pick the packages folder and reinstall on of the files starting with “iPhone XXX.pkg” it will reinstall the files - i think it was the “iPhoneSystemComponents.pkg” one, but im not 100% sure on that one -but if you try a few it should replenish the photos in your library!
hope this help!
james
Good example. I’ve been playing around with this and it works well in the simulator picking from the list.
I added a button on the last view to bring the picker back up and allow the user to pick another image or take another image. While this works fine in the simulator it crashes the app after taking a picture with the camera for the second time.
I was wondering if anyone has actually tried this on the phone yet with the camera… and was able to make picker display and work more than just once in the app. I’m beginning to fear there is a defect still in apples code.
Just another note to mention here it does not appear that you can attach the the UIImagePicker to anything other than the top window of the application. Doesn’t seem to work correctly as a modelviewController either. =/ This camera thing is getting rather frustrating…
I’m having problems getting this demo to work. I’m using Beta 5 and when I compile I get “error: There is no SDK with specified name or path ‘Unknown Path’”. So, I built a new project and transplanted the class from this demo into the new project. The demo launches, but all I see is a white screen. No image picking. (this is in the simulator) Any ideas?
Sorry to respond to my original post, but I found an answer to my question and maybe it will help someone else out. For Beta 5, this line in the code needs to be commented out:
self.window = [[UIWindow alloc initWithFrame:[UIScreen mainScreen] bounds]] autorelease;
I found the solution via comment #4 on this site:
http://www.iphonedevforums.com/forum/showthread.php?t=85
Hey this is Mac Tyler the creator of iPhoneDevForums.com, I just wanted to let you know that I am very happy I could help you guys figure out your problem. Stay tuned as we plan on major updates for the site. As alway,s we encourage you to post any comments or questions that you have and we will try our best to answer them!
Thanks,
Mac Tyler
Thanks, great article.
Any idea on how to get an image back *into* the Photo Album?
What I would like to do is import an image via UIImagePickerController, modify
that image, and then put the result back into the Photo Album. But I don’t
see any interfaces that allow that.
Never mind. The call to do this is:
UIImageWriteToSavedPhotosAlbum(image, self, (SEL)@selector(image:didFinishSavingWithError:contextInfo:), nil);
Amazing what one can learn by reading the .h file ;}
I downloaded the project but it wouldn’t build for me, so I started a fresh iPhone OS “Window Based Application” named PickImage. I copied the source code from the downloaded project into the files created by xcode and it worked right away.
I found that, doing this, the first line of applicationDidFinish…. is unnecessary.
Remove this and it works (and avoids leaking a Window):
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
is it possible to read a pixel value, any pixel on screen, while the camera is open (before the photo taken)? How to do that? please be nice and clear, this is my first week creating iPhone apps…
How can one enumerate the photos in the iphone photo libraries - on the simulator and on the actual device. In this “imagePicker” application once a photo is selected if i find the current directory and then run the directory enumerator on this - my objects returned are nil.