How To Select Image From Library In iPhone

In this article we will see how to image select from phone gallery and display the same image in the application. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “SelectImage”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the
directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and
notice the template generated a separate nib, SelectImage ViewController.xib for the SelectImage application.

Step 4: Open the SelectImageViewController.h file and make the following changes in the file:

 

#import <uikit/UIKit.h>
@interface SelectImageViewController : UIViewController
< UIImagePickerControllerDelegate , UINavigationControllerDelegate>
{
IBOutlet UIImageView *imageSelect;
IBOutlet UIButton *libraryButton;
}
@property(nonatomic,retain) UIImageView *imageSelect;
@property(nonatomic,retain) UIButton *libraryButton;
-(IBAction)LibraryPicture;
@end

 

 

Step 5: Double click the SelectImageViewController.xib file and open it to the Interface Builder. Firs drag the ImageView from the libraryand place it to the view window. Connect File’s Owner icon to the UIImageView and select imageSelect. Now drag the button from thelibrary and place it to the view window, select button and bring up Connection Inspector connect Touch Up Inside to the File’s Owner iconand select LibraryPicture. Now save the .xib file and go back to the Xcode.

Step 6: Open the SelectImageViewController.m file and change the following changes:

 

#import "SelectImageViewController.h"
@implementation SelectImageViewController
@synthesize imageSelect;
@synthesize libraryButton;
- (void)dealloc
{
[imageSelect release];
//[takePictureButton release];
[libraryButton release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(IBAction)LibraryPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageSelect.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

 

 

Step 7: Now compile and run the application on the Simulator.

You can Download SourceCode from SelectImage

Leave a Comment:

6 comments
How to select image from Library in iPhone | iPhone / iPod / iPad Software developing says December 21, 2011

[…] iPhone Tutorial | iPhone iOS4 iPad SDK Development & Programming Blog This entry was posted in IOS Development and tagged from, Image, iPhone, Library, select by sniuff. Bookmark the permalink. […]

Reply
Hiro says January 19, 2012

Hi,

After the successful build of this sample code, I see no photo displayed on my simulator window. I made sure that there are photos on my iPhone Library folder. I also changed from UIImagePickerControllerSourceTypePhotoLibrary to UIImagePickerControllerSourceTypeSavedPhotosAlbum but with the same result. I supposed this works with the simulator. Is there anything additional to be done?

Thanks, Hiro

Reply
Chinmay says July 16, 2012

Why there is need for if condition at the start of the -(IBAction) librarypicture method ?What does it check for?

Reply
Rste says January 8, 2013

I tried this and the build fails.

In the SelectImageViewController.m file it says:
ARC forbids explicit message send of ‘release’

&

ARC forbids explicit message send of ‘dealoc’

Any ideas why it fails when I build?
Thanks,
R

Reply
Rste says January 8, 2013

The downloadable source does work so I’ll trying looking at the differences.

Reply
davidbkh says September 1, 2013

@Rste: it is normal because this source code is for XCode 3. ARC behaves differently with XCode 4, you don’t have to worry about the memory (except if you do complicated things). You should try to find another tutorial, this one won’t work in your case.

Reply
Add Your Reply