iPhone Pick images 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.

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;

-(IBAction)openPhotoLibrary:(id)sender;

@end

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

-(IBAction)openPhotoLibrary:(id)sender
{

UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Photo Library"
otherButtonTitles:nil];
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 {
}

@end

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

Ads