Tuesday, 2 April 2013

Taking image from photo gallery in iOS app

To Take image from photo gallery you will have to implement a method of UIImagePickerControllerDelegate.

First of all go to .h file and defines the  UIImagePickerControllerDelegate.


@interface NewPersonViewController : UITableViewController 


Now go to .m file and implement the delegate method.
This method will be called when you will select your desire photo from image gallery, But right now this method will not called.
There should be an ImageView on your controller on which you will show image after selecting from gallery and make the property of that imageView.


- (void)imagePickerController:(UIImagePickerController *)photoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *selectedImage = [info valueForKey:UIImagePickerControllerOriginalImage];
    [self.selectedImageView setImage:selectedImage];
    [photoPicker dismissModalViewControllerAnimated:YES];
}

   
 
This code will not work according to your desire. You need to write an IBAction which will connect to a button and on pressing button photo gallery will be opened and you will have to write the related code in this method.



- (IBAction)PhotoFromAlbum:(id)sender {
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:photoPicker animated:YES completion:NULL];
}


Now this above delegate method will also be called and whole code will work fine.

No comments:

Post a Comment