Picking Images with the iPhone SDK UIImagePickerController

The 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.

iPhone Image Picker Photo Album Screenshot

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.

iPhone Image Picker Photo Selection Screenshot

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.

iPhone Image Picker Full Screen Image Screenshot

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.

PickImage Source Code (.dmg)

Share and Enjoy:
  • StumbleUpon
  • Digg
  • Reddit
  • del.icio.us
  • Suggest to Techmeme via Twitter
  • Technorati
  • Slashdot
  • HackerNews
  • Twitter
  • Facebook
  • Print
You can leave a response, or trackback from your own site.

32 Responses to “Picking Images with the iPhone SDK UIImagePickerController”

  1. This site looks promising for info on the iPhone SDK! I’m going to have to look at this abit closer!

  2. pete says:

    Thanks for stopping by Orville!

  3. David Manpearl says:

    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

  4. pete says:

    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.

  5. [...] the Picking Images with the iPhone SDK UIImagePickerController article I gave some sample code using the UIImagePickerController to get images from your photo [...]

  6. curtis says:

    Great article!

    Do you know how to scroll through the photoalbum?
    How can you flick to the next image?

    thanks,

  7. Mauro says:

    Great post!
    just a question, my simulator Photo DB is empty…
    how can I put images on it ?
    thanks

  8. James Churchman says:

    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

  9. Mac says:

    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.

  10. Mac says:

    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…

  11. Ricket says:

    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?

  12. Ricket says:

    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

  13. Mac Tyler says:

    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

  14. christopher minson says:

    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.

  15. christopher minson says:

    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 ;}

  16. Charles Lloyd says:

    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];

  17. mike says:

    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… :-)

  18. engineer 2008 says:

    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.

  19. Hi buddy,

    I tried you sample code of imagePickerControl. Its works fine in my simulator.

    The thing is that i want to upload that image to my .Net server. I have a page in my server which get the input stream and save it as a image in server path.

    How to do the image upload in iphone SDK.

    Help me.

  20. ROOT says:

    would like to see an example that saves images to directory with RANDOM file names…

    And maybe the pup file updated to display them on a server side page..

  21. daniel says:

    Great sample app! Thanks!

  22. chandrika.bhat says:

    Comment by Mauro
    2008-04-22 09:22:53
    Great post!
    just a question, my simulator Photo DB is empty…
    how can I put images on it ?
    thanks

    ———–
    great Smaple…..

    We can save images in iPhoto Library of a simulator…to test this app.
    1.Go to Safari
    2.Google for required images
    3.click for 5 secinds on the images…
    4. it will ask for saving the images..
    5.Now we can test this app very easily

  23. S.Lakshmikanth Reddy says:

    Hi everybody,

    i have a doubt here ,i have selected the image from the photo library and saved it to my imageView.image property, now is it possible to edit this image anyway and send it through e-mail. Please if anyone can help me out in this issue it is greatly appreciable.

  24. mamatha says:

    nice tutorial!!!!!!!!!!
    it is very much helpful

  25. Antje says:

    This has a memory leak for the initialization:

    [[UIImagePickerController alloc] init];

    Does anyone have a solution for this?

  26. raj says:

    I cannot build and go this project. It doesnt allow me to select Iphonesdk and gives error saying no valid sdk..anybody else had this problem and what did they do.

    WHat is the procedure to execute the source code given in this page.

    Thank you for your help.

  27. Brian says:

    I know this is an old post, but I was just wondering if anyone happens to know if it is possible to use the UIImagePicker to automatically select a random photo from the photo library, rather than manually picking one?

  28. SUmir says:

    Hey any Geek knows how to have slideshow with picker (traversing next and previous images on flick event)

  29. SUmir says:

    I read now it is possible in iphone 4 sdk as photo library acess is given.But i donno how to use it for making slideshow using picker

  30. Jay says:

    So I can take a picture and write it to the photo album, but what I want to do on the next startup of the app is display that picture again.

    So after taking the picture and saving it, can what’s the best way to grab it next time I start up. Is there a path to it I can access?

    Or do I need to save a copy of on the bundle path to access later.

    Thanks,
    Jay

  31. Andrey says:

    Tell me please how can I download pictures to your iphone_sdk_3.1.3

    Thanks,
    Andrey

  32. Andrey says:

    Tell me please how can I download pictures to your iphone_sdk_3.1.3

    Thanks,
    Andrey

Leave a Reply

*