Sunday, May 1, 2011

Use of Image Picker in iPhone SDK


Hello Friends now we will start to learn about image handling in iPhone. Apple has provided us most of the stuff ready for the use.  UIImagePicker is best example of that. We just have to invoke the function for the picking image from the iphone or to take image from camera.  Any novice developer can use this library easily.
Methods of picking images from iPhone are deprecated. This method is available from iOS 3.0.
 Let’s start this stuff with View based Application.
  • Create View Based Application and Give a name to it.
  • Create IBOutlet and IBAction in ImageViewController.h. For that write following code  ImageViewController.h.
@interface ImageViewController : UIViewController < UIImagePickerControllerDelegate,UINavigationControllerDelegate > 
{
               UIImageView *imageView;
               UIButton *selectPhotobtn;
               UIButton *takePhotobtn;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIButton *selectPhotobtn;
@property (nonatomic, retain) IBOutlet UIButton *takePhotobtn;

-(IBAction) getPhoto:(id) sender;
@end


        
UIImagePickerControlDelegate and the UINavigationControllerDelegate are needed for properly interface with the image picker. 
  •  Create interface by opening ImageViewController.xib in the interface builder. 
Ø  Drag a UIImageView on the main view.
Ø  Set the Mode of the UIImageView to Aspect Fit in the Attribute inspector.
Ø  Drag a UIButton on to the view and title it Select Photo.
Ø  Drag another UIButton on to the view and title it Take Photo.

  • Connect the IBoutlets and IBAction

Ø  Connect selectPhotobtn to the UIButton titled Select Photo.
Ø  Connect takePhotobtn to the UIButton titled Take Photo.
Ø  Connect the imageView to the UIImageView.
Ø  Connect the Touch Up Inside Callback on each of the buttons to the getPhoto method.

  •   Now we have to implement the code for the getPhoto method.

@synthesize imageView,selectPhotobtn,takePhotobtn;
 
-(IBAction) getPhoto:(id) sender {
   UIImagePickerController * picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
 
   if((UIButton *) sender == selectPhotobtn) 
   {
           picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
   } 
   else
   {
          picker.sourceType = UIImagePickerControllerSourceTypeCamera;
   }
  [self presentModalViewController:picker animated:YES];
}


  • Once a photo is selected, the ImagePicker will callback to a method in our class called didFinishPickingMediaWithInfo.  This is a delegate method. Add the following code to ImageViewController.m file.

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 [picker dismissModalViewControllerAnimated:YES];
 imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}


In iOS everything is so finely designed that one can understand function use from its name. Only this stuff can create very good demo application in iPhone.

No comments:

Post a Comment