iPhone

iPhone

Info on Apple's new iPhone. Photos and blog reviews. The iPhone was announced in January but will not be available till June 2007.

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)

[del.icio.us] [Digg] [Reddit] [Technorati]
Sponsors
Comments
i am trying the same thing... i can see from your screen shots, that you are having the same trouble as me. the selected image is stretched vertically. if the image is a portrait image, then it goes off the top of the screen. do you know what is causing this?
iphone sdk | How to Animate keyboard in iphone | show/Hide keyboard http://iphone-2-0.blogspot.com/2008/08/iphone-sdk-hide-keyboard.html iphone sdk | How to Animate keyboard in iphone | show/Hide keyboard http://iphone-2-0.blogspot.com/2008/08/iphone-sdk-hide-keyboard.html
Awesome article exactly what i'm looking for. First is this testing on 2.1 or only 2.0. Cause when I change the UIImagePickerControllerSourceTypePhotoLibrary to UIImagePickerControllerSourceTypeCamera the program crashes and burns. Also, does this work in the simulator for you. It doesn't seem to want to work for me even in 2.0 but atleast it doesn't crash. Any advice would be helpful
Excellent article! I am using the sdk 2.1 and used a new window project. I had to make 2 modifications to get this to work: 1)modified @interface PickImageAppDelegate : NSObject 2)commented out: self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; albert c.
Hi, Is apple providing any APIs for zoom in and zoom out, when we have launched the camera?
Add a Comment:
Already a member? Log In
Sponsors
About the Author

0 Kudos
Top Geek Articles
Why every guy should buy their girlfriend Wii Fit.
Gratuitous...
Astronomy Picture Of The Day
This picture makes us feel very very small.
Hottest Girl Superhero List
A list of female video-game characters you should check out.
More From Zimbio
Copyright © 2009 - Zimbio, Inc. Some rights reserved.