This is part of an ELC Tech Network

Getting Images From The iPhone Photo Library Or Camera Using UIImagePickerController

This will be a simple tutorial showing you how to access the iPhone’s photo library as well as the camera. Since the 3.0 update, the methods for picking photos have been deprecated. So this will be a 3.0 and above tutorial.

We will be creating an applicaiton that will allow you to pick a photo from the library or camera and display it on the screen. Here is a screenshot of what the app will look like:

photo 2

Let’s go ahead and get started…

1. Create A New View Based Application

I called mine photoApp (I will be using this name as reference)

2. Create The IBOutlets and IBAction

Open photoAppViewController.h and add the following code

#import 
 
@interface PhotoAppViewController : UIViewController | UIImagePickerControllerDelegate, UINavigationControllerDelegate | {
	UIImageView * imageView;
	UIButton * choosePhotoBtn;
	UIButton * takePhotoBtn;
}
 
@property (nonatomic, retain) IBOutlet UIImageView * imageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhotoBtn;
@property (nonatomic, retain) IBOutlet UIButton * takePhotoBtn;
 
-(IBAction) getPhoto:(id) sender;
 
@end

Important: Replace the | in the interface declaration with < and >.  I just used the vertical pipe bc wordpress was replacing it with html encoding.

Notice that we implement the UIImagePickerControlDelegate and the UINavigationControllerDelegate. These are both needed to properly interface with the image picker. The rest of this stuff should be pretty strait forward if you have been reading our tutorials. We set up some outlets to the buttons we are using (this will be to determine which button was pressed). There is also and IBAction that will get called when the user presses either of the buttons. This method (getPhoto) will show the ImagePicker.

3. Create The Interface

Open up photoAppViewController.xib in Interface builder and follow these steps:

  1. Drag a UIImageView on to the main view
  2. Set the Mode of the UIImageView to Aspect Fit in the Attribute inspector
  3. Drag a UIButton on to the view and title it Choose Photo
  4. Drag another UIButton on to the view and title it Take Photo

The interface should look something like this:

screenshot_01

4. Connect The IBoutlets and IBAction

  1. Connect choosePhotoBtn to the UIButton titled Choose Photo
  2. Connect takePhotoBtn to the UIButton titled Take Photo
  3. Connect the imageView to the UIImageView
  4. Connect the Touch Up Inside callback on each of the buttons to the getPhoto method

When you click on File’s Owner the connection inspector should look like this:

screenshot_01

Advertisement

Close Interface Builder

5. Implement The getPhoto Method

Open PhotoAppViewController.m and add the following code:

@synthesize imageView,choosePhotoBtn, takePhotoBtn;
 
-(IBAction) getPhoto:(id) sender {
	UIImagePickerController * picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
 
	if((UIButton *) sender == choosePhotoBtn) {
		picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
	} else {
		picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	}
 
	[self presentModalViewController:picker animated:YES];
}

Make sure you synthesize your view properties.  Here is what is going on in this method.

We first create a new UIImagePickerController object.  This is a view controller and can be displayed any way you would normally display a view controller (pop on to a navigation view stack, load in a tab view, present as modalviewcontroller).  Next, we set the delegate of the picker to our viewController.  This just means the picker will call a method inside of this class when the user picks a photo.

Next, we determine which button was pressed.  Since both buttons were connected to this method, we can see which one called it by using ==.  Now, here is where Apple has done a great job.  The difference between displaying the camera and photo library comes from setting a single property in the picker.  Looking at the code, it should be pretty obvious which is which.

Finally, we call presentModalViewController with our picker.  This will animate the picker into view from the bottom of the screen to the top.  Depending on the button you press, you should see one of the views below:

photo 3photo

6. Displaying The Selected Image

Once a photo is selected or taken, the ImagePicker will callback to a method in our class called didFinishPickingMediaWithInfo.  Add the following code to your PhotoAppViewController.m file.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	[picker dismissModalViewControllerAnimated:YES];
	imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

The first line just hides the picker.  The next sets to image property of our image view to an image returned from the picker.  The picker actually returns an NSDictionary.  That is because the other key UIImagePickerControllerMediaType; will return whether this is a video or an image.

And there you have it.  A way to get photos from the iPhone’s image library or camera.  If you have any comments or questions, feel free to write them in the comments section of this post or write them to me on twitter.  You can download the source below. Happy iCoding!

iPhone Tutorial – PhotoApp.zip

This entry was posted in Uncategorized. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

42 Comments

  1. Jeremy
    Posted July 28, 2009 at 6:30 pm | Permalink

    great tutorial. thanks!

  2. Ray
    Posted July 28, 2009 at 11:22 pm | Permalink

    Awesome tutorial. Thanks for keeping it simple and concise.

  3. John
    Posted July 29, 2009 at 5:23 am | Permalink

    Thanks for that Brandon. Timely as well. Am currently struggling with some issues relating to use of the camera.

    Any ideas about how to check, and then correct if necessary, the orientation of a photo. I am passing it through UIImageJPEGrepresentation to post as data via an HTML post but it disregards the orientation of the photo when doing this.

  4. Zach
    Posted July 29, 2009 at 8:37 am | Permalink

    You have a memory leak – you never release your picker object.

  5. Marcelo
    Posted July 29, 2009 at 9:21 am | Permalink

    Hey Brandon, nice tutorial.

    One question: Once you try to take a picture I get a message:
    “__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__”. Even with your sourcecode. Why’s that?

    Thanks.

    • Leo
      Posted April 22, 2010 at 7:15 am | Permalink

      Run this app on your iPhone, since this app uses camera. If you run this app in the simulator, then you will get this exception.

  6. Posted July 29, 2009 at 10:11 am | Permalink

    @Marcelo,

    Hrm… I don’t know. Are you compiling it for 3.0?

  7. Marcelo
    Posted July 29, 2009 at 11:41 am | Permalink

    Aye, compiling with 3.0 – Simulator. No clue though what is causing this.

  8. Posted July 29, 2009 at 2:38 pm | Permalink

    Thanks !

    Do you think is possible overlay a hat image while taking a photo ?

  9. Zachary
    Posted July 29, 2009 at 3:15 pm | Permalink

    Nice! I’ve been lookin for some tutorials like this, I was wondering if you guys could post one on using a slider to adjust the iPhone or iPod’s system volume, and how to access music player controls? Those ones would be fun to check out

    • Posted April 14, 2010 at 9:08 am | Permalink


      MPVolumeView *volumeview = [[MPVolumeView alloc] init];
      [volumeview setFrame:CGRectMake(10, 360, 300, 34)];
      [self.view addSubview:volumeview];
      [volumeview release];

  10. Lars
    Posted July 30, 2009 at 7:40 am | Permalink

    @marcelo:

    I dont think the ssimulator can use theese methods.. Try compilig for device!

  11. AJ
    Posted August 8, 2009 at 12:22 am | Permalink

    Brandon,

    Nice tutorial. I tweaked it and set the mediaType of picker controller to kUTTypeMedia which shows only the video recorder – which is what my requirement is. (This is for 3GS)

    However I am stuck at one place. I want the camera/recorder to start recording automatically when my showPicker method is called.

    Any clue how I can achieve this would be a great help!

    Thanks,
    AJ

  12. Aaron
    Posted August 11, 2009 at 7:10 pm | Permalink

    I’m getting the uncaught exception, too.

  13. Posted August 13, 2009 at 1:38 pm | Permalink

    How can you save the image into the gallery folder of the iphone after you hit the “Use” button on the camera interface?
    Thanks in advance

  14. Posted August 19, 2009 at 5:17 am | Permalink

    Thanks for the tutorial! Works great

  15. Posted August 19, 2009 at 6:11 am | Permalink

    great tutorial! Thank you. Check out iPhoneToot.com for more tutorials!

  16. jamey
    Posted August 19, 2009 at 12:28 pm | Permalink

    In 3.0, after hitting the “Use” button after taking a picture, there is NO progress indication while the image is being saved. Has anyone figured out how to create a callback when the “Use” button is selected? Given that it takes a few seconds to process the image after hitting “Use” I want to display some sort of progress(activity) indicator during this time.

    Thanks for the useful post and code!

  17. fdqps
    Posted August 25, 2009 at 11:15 am | Permalink

    Hi,

    I have 3.1 installed (SDK, iPhone and on a 3Gs) but only get the camera icon not the video recorder icon.

    Any clues?

    Best regards @fdqps

  18. Ryan
    Posted August 27, 2009 at 12:53 am | Permalink

    @Marcelo, that is because you are running this on simulator, if I am correct…

    Anybody know how I can save the image for later use in the app then (in a UIImageView). Please, this is the last little addittion I need for my app and it’ll be a few weeks of beta testing and little interface improvements away from hitting the app store, so any points in the right direction will be greatly appreciated!

  19. Karl
    Posted August 28, 2009 at 1:16 am | Permalink

    Hi,
    I’m new to all this. I’ve tried creating this app and everything is fine. BNut when I press Build and Go the Simulator launches but when it tries to launch the app finder give me a message saying it has crashed. Any ideas why?

  20. Radikal Edward
    Posted September 15, 2009 at 11:25 am | Permalink

    hi,

    Have you seen the attach effect when run Photos app and you select send by email option, that when all the background turn in dark, and make the image tiny, and then, attach the image in the Mail App, What you think about it!!, how could i do the same?…thanks

  21. Jeff
    Posted October 2, 2009 at 9:07 am | Permalink

    Is there anyway to set the picked image as iPhone wallpaper?

  22. Posted October 14, 2009 at 5:55 am | Permalink

    The White silicon case for Ipod Touch is simply class and more stylish and makes the Ipod Touch good loking. This way you can keep your Ipod Touch safe and scratch resistant.

  23. Posted October 14, 2009 at 7:20 am | Permalink

    HI
    Does any one know how to get the cropped image from library.

    Thanks

  24. Posted October 21, 2009 at 4:07 am | Permalink

    Just wanted to know if there is a way i can get my own images which are not in the photo library?
    The thing is, I am getting my images from a database. so how do I populate an image picker from the images in the database?
    a tutorial would be really helpful.
    Thanks in advance

  25. iphonedev
    Posted November 5, 2009 at 9:42 am | Permalink

    i have a question
    can i access the whole address book at once and maybe send data using a http request

    does apple allow us to do that

  26. Tomy Carey
    Posted November 11, 2009 at 9:52 am | Permalink

    Great tutorial! I was wondering if somebody knows how I can use the same idea to take a picture for example, and then attach it to a in app email?? thanx!

  27. Posted November 19, 2009 at 1:35 am | Permalink

    It was awesome! However, I am a novice unfortunatelly.

    If I want to see pictures through a simulator, what directory do I put pictures into?

    I don’t have iPod and iPhone now.

  28. Satyam
    Posted December 8, 2009 at 5:38 am | Permalink

    Hi AJ,
    You posted “Nice tutorial. I tweaked it and set the mediaType of picker controller to kUTTypeMedia which shows only the video recorder – which is what my requirement is. (This is for 3GS)”
    Can you share your code with me. My email id is “satyam90@indiatimes.com” I’m struggling with recorder tutorial.
    Thanks,
    Satyam.

  29. Tarun Sharma
    Posted January 14, 2010 at 12:36 am | Permalink

    Hello,

    Thanks for the tutorial. I am using UIImagePickerController from one of my application and facing very strange problem. When i select an image from library and display it in imageview the memory consumption become very high (from 2.5MB to 12.9 MB, incase i select heavy image) and never came down again. If i comment the line which display the image in imageview the memory does not rise up at all. Can you please help me on this as i m really struck on this issue.

    What i exactly do when selected image from imagepicker controller i saved it in application delegate UIimage type variable and then display it using setimage.

    Thanks
    Tarun sharma

  30. Satya
    Posted January 22, 2010 at 12:59 am | Permalink

    Hi Brandon,

    I have small doubt, can you clarify,

    I want to get images with thier names, with out using imagePickerController. How can I?

    Please Help me.

    Thanks,
    Satya

  31. Posted January 28, 2010 at 10:15 pm | Permalink

    This is so awsome…cant wait to bookmark this post, and I think I should feed your blog….have a good day,man.

  32. Posted February 9, 2010 at 11:19 pm | Permalink

    Thanks so much for this. Your tutorials are by far the best on the web and are very easy to follow- plus you provide a download to the source which is really helpful for debugging :)

    Do you have a link to donate funds?

  33. Russell
    Posted February 17, 2010 at 3:43 am | Permalink

    Hi I am wondering how do I send the picture to UIWebView?

  34. Neak
    Posted February 22, 2010 at 12:12 am | Permalink

    Hi, Brandon
    when I invoke [self presentModalViewController:picker animated:YES] method, could I get a video recording pop-up window instead of a still picture camera view?

  35. Siddharth
    Posted February 22, 2010 at 12:07 pm | Permalink

    Hi I’m facing an issue…

    After selecting an image from the saved photos library, it does no load up onto the image view.

    The screen stays blank. Any idea why?

    PS: Testing on iPod Touch and I’ve followed the tutorial as specified. There are no compilation errors thrown and no warnings either.

  36. kaps
    Posted March 9, 2010 at 10:00 am | Permalink

    Nice tutorial .I am new in iPhone development and need to build app just like ‘Photos’ in iPhone(Picking images and slideshow…) so pls help me how to build silde show on UIImagePicker……like after didFinishPikickingImge how to get image array so that can start slide show……pls give ur valueable suggestion….

  37. gnat
    Posted April 22, 2010 at 12:33 pm | Permalink

    Great tutorial,
    Any idea on how to put all the images from a particular photo album into an array? I am looking to do a slide show but cant find any info on how to access multiple photos. Want have a user select an album of photos from the uiimagepickercontroller.

  38. vikrant patel
    Posted June 18, 2010 at 5:03 pm | Permalink

    hi, this is awsome tutorial….. thanx buddy…….. have a nice day……..

  39. seihee
    Posted June 29, 2010 at 12:07 am | Permalink

    Great job!
    it’s KOOOOOOOOOOOOOOOOOOOOOOL

  40. nir
    Posted July 2, 2010 at 9:36 am | Permalink

    Nice tutorial
    But i want a button in this to display next images
    how can i get this?
    plz reply

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">