from Trails in the Sand blog
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.
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.
