Share on Google+Share on Google+

iPhone Pick images from Photo Library

In this example application we are going to show you how to pick an image from photo library.

iPhone Pick images from Photo Library

In this example application we are going to show you how to pick an image from photo library.

To choose the photo from the photo library we required to call three different delegates in the @interface class.

1.UINavigationControllerDelegate - We required this method to perform some actions such as pushed and popped that would not be on the scope of your view controller.

2.UIActionSheetDelegate - implements the button actions and any other custom behavior.

3.UIImagePickerControllerDelegate - This methods of this protocol notify your delegate when the user either picks an image or movie, or cancels the picker operation.

a Simple Example:
Header File - PhotoLibraryViewController.h

#import <UIKit/UIKit.h>

@interface PhotoLibraryViewController : UIViewController <UINavigationControllerDelegate ,UIActionSheetDelegate, UIImagePickerControllerDelegate>{

IBOutlet UIButton* openLibrary;

@property(nonatomic, retain)IBOutlet UIButton* openLibrary;



Implementation file - PhotoLibraryViewController.m

#import "PhotoLibraryViewController.h"
#import <AudioToolbox/AudioServices.h>

@implementation PhotoLibraryViewController
@synthesize openLibrary;

- (void)dealloc {
[super dealloc];
[openLibrary release];

- (void)viewDidLoad {
[super viewDidLoad];


UIActionSheet *actionSheet = [[UIActionSheet alloc]
destructiveButtonTitle:@"Photo Library"
actionSheet.actionSheetStyle = UIBarStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

if (buttonIndex == 0)
NSLog(@"ok One");

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
[picker release];


if (!(buttonIndex == [actionSheet cancelButtonIndex])) {
NSString *msg = nil;

[msg release];
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

- (void)viewDidUnload {


In the example, we are opening a photo library on button click. So you need to take a button at nib file and connect it with the appropriate action in file owner.
Run the application.. the output should be similar to given image.

Download code


Posted on: September 22, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null