UIImagePickerController tutorial in IOS

UIImagePickerController

Tags: #UIImagePickerController #IOS #iPhone SDK #IOS8

Images in iPhone are saved in gallery of the device.Since every app in iPhone has sandbox environment(did not interfere with other apps and has its own directory to save videos, photos, database etc.).

In iPhone app development, we certainly need images for sharing with our friends or modifying those pictures into funny art animation, decorate those pictures with beautiful frames.For this, objective C provides us with UIImagePickerController that provide us interface to select picture or take pictures using camera.

For this i had created an easy to use classes named as PictureChoser, which allows you to simply call its method addImagePickerToNavigationController to add UIImagePicker to your view.

The full declaration of this method is

-(void)addImagePickerToNavigationController:(UINavigationController *)navController withSourceType:(NSString *)sourceType

Here it takes two parameters one your navigationController to which you want to add image Picker, and second is source type which takes string as its value. We will pass three type of values in source type

1) Camera (will open up camera to take pictures)
2) photoalbums (will open up albums of our gallery)
3) library (will open up all photo library)
Let’s jump on to the details, that how can we use them in our project.
First download files from link given at the end of the post, and drag all the files to your project.
Now import PictureChoser.h to your header file where you want to add ImagePicker functionality

 #import “PictureChoser.h”

And declare it delegate as shown below

@interface AddFeedViewController : UIViewController

Now create an instance of PictureChoser

 PictureChoser *picChoser;
Open up your .m file and in your button action add the below code

 picChoser = [[PictureChoser alloc] init];
        picChoser.delegate=self;
        if ([sender tag]==1)
        {
            [picChoser addImagePickerToNavigationController:self.navigationController withSourceType:@”library”];
        }
        else
        {
            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
            {
                [picChoser addImagePickerToNavigationController:self.navigationController withSourceType:@”Camera”];
            }
            else
            {
                [AppDelegate showAlertWithTittle:@”Sorry!” andMessage:@”Device does not has camera.”];
            }
        }

In the above code, first we create an instance of  PictureChoser by alloc and init methods.Then we set its delegates to self, so that our class can be notified when user select/take an image.Then we call our method addImagePickerToNavigationController to add picker to our view and present an UIIMagePickerController to the app user.Then we categorise which thing to be displayed when user press button either camera will open up or library of photos.I user chose camera then we check if camera is available for device or not.(In case of simulator so that our won’t crash).
Now we will implement our delegates
#pragma mark PictureChoser Delegates
-(void)setImage:(id)info
{
    businessImageView.image=[info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *temp = [info objectForKey:UIImagePickerControllerEditedImage];
    [picChoser release];
}
-(void)cancel
{
    [picChoser release];
}
The setImage:(id)Info will be called every time a user select/take picture.I used allowEditing in my PictureChoser.m file that’s why i set objectForKey:UIImagePickerControllerEditedImage. If you want normal picture without editing then you can set allowsEditing property in PictureChoser.m to NO and set key value to UIImagePickerControllerOriginalImage.

Below is the link to download files.

PictureChoser Files