Home > DeveloperSection > Articles > iOS : Sample on UIImagePickerController

iOS : Sample on UIImagePickerController


iPhone iPhone  Ios  Objective C 
Ratings:
0 Comment(s)
 776  View(s)
Rate this:

iOS : Sample on UIImagePickerController

 

Here we will learn about UIImagePickerController class, it is an UI component. It is a user interface that provides the functionality to interact with the camera. If we are creating an app and want to enable camera feature in our app then at this situation we will use UIImagePickerController.

 

Using UIImagePickerController we can make our app to be able to pick image from device and also we capture image through device camera. A condition is here it will work if the device has a camera other wise it will display error.

UIImagePickerController class have some important methods that we will implement in our app, are follows:


1.    UIImagePickerControllerSourceTypeCamera: this method is used to pick image from camera.

2.    UIImagePickerControllerSourceTypePhotoLibrary: this method is used to pick image from the gallery.

3.    UIImagePickerControllerSourceTypeSavedPhotosAlbum: this method is used to pick image from photos album/camera roll.

 

Now, in this article we are going to grab the picked image or take a new one if the device has a camera and load it to an image view.

Now, to create app using UIImagePickerController follow the steps:


Ø  Open the Xcode go to File > New > Project > here select 'Single View Application' and click 'Next' > now enter the Product Name as 'ImagePickerSample' and click 'Next' and after click 'Create' button.

 

Ø  Now, select ViewController.xib file and add UIView on the ViewController and add another object UIImageView on the UIView using drag and drop, provide the space at the bottom for the button.

 

Ø  Now, add two buttons on the bottom of the storyboard, not on the UIView and named left button as 'Browse Image' and right button as 'Camera'.

 

Ø  Now, select 'Assistant Editor' it will display two views on the Xcode, in one view open ViewController.h file and open ViewController.xib file in another.

 

Ø  Now, create a property with pressing 'Ctrl' key click and drag UIImageView to ViewController.h file between the @interface.... @end definition and named it 'imageView' with IBOutlet selected.

 

Ø  Now, same as above drag both buttons one by one and named it 'browseImage', 'takeImage' with IBAction selected because we will generate action by clicking both buttons. browseimage action method takes the action to picking image from gallery and takeImage method is used to open the camera to take image.

 

Ø  Now, define the 'UIImagePickerControllerDelegate' and 'UINavigationController' in 'ViewController.h' file. After implementing all code in the 'ViewController.h' file will look like this:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate,

UINavigationControllerDelegate>

 

   @property (strong, nonatomic) IBOutlet UIImageView *imageView;

    - (IBAction)browseImage:(id)sender;

    - (IBAction)takeImage:(id)sender;

 

@end

 

Ø  Now, select 'ViewController.m' file, both the methods that we created in 'ViewController.h' file are already implemented here. Ok, inside the action method of browseImage:, initialize the 'UIImagePickerController' object, set its delegate to self, and also set its sourceType to UIImagePickerControllerSourceTypePhotoLibrary, and call its presentViewController: with the UIImagePickerController object as a parameter.

 

Ø  Now, same as browseImage implement the code in the takeImage: action method but here is one difference that is the sourceType, here sourceType will be UIImagePickerControllerSourceTypeCamera, code will look like this:


- (IBAction)browseImage:(id)sender {

 

                 UIImagePickerController *picker = [[UIImagePickerController alloc]init];

                picker.delegate = self;

                picker.allowsEditing = YES;

                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                [self presentViewController:picker animated:YES completion:NULL];

 

          }

 

     - (IBAction)takeImage:(id)sender {

        

NSLog(@"Selecting Camera");

          UIImagePickerController *picker = [[UIImagePickerController alloc]init];

          picker.delegate = self;

          picker.allowsEditing = YES;

          picker.sourceType = UIImagePickerControllerSourceTypeCamera;

          [self presentViewController:picker animated:YES completion:NULL];

 

}

 


10.  
Here we see another method 'imagePickerController:', this is a delegate method of UIImagePickerControllerDelegate. It tells the delegate that the user picked a still image. Our delegate object's implementation of this method should pass the specified media on to any custom code that needs it, and should then dismiss the picker view.

When editing is enabled, the image picker view presents the user with a preview of the currently selected image along with controls for modifying it, here is the code:


- (void) imagePickerController:(UIImagePickerController *)picker

didFinishPickingMediaWithInfo:(NSDictionary *)info {

 

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

self.imageView.image = chosenImage;

          [picker dismissViewControllerAnimated:YES completion:NULL];

 

     }

 

11.  here is another method, if you cancel ImagePickerController then 'imagePickerControllerDidCancel:' method will be called to dismiss UIImagePickerController in that method, here is the code:

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {

 

                [picker dismissViewControllerAnimated:YES completion:NULL];

 

          }

 

 If the device does't have camera then the problem can be occurred so to prevent this we will check the camera is available or not, to check this we will implement a UIAlertView in the 'viewDidLoad:' method, when the camera will not available then it will alert a message ‘Device has no camera’, here is the code:


if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {

 

      UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"

                                                                                     message:@"Device has no camera"

                                                                                       delegate:nil

                                                                          cancelButtonTitle:@"OK"

                                                                          otherButtonTitles: nil];

      [myAlertView show];

 

}

Another important thing is that this app (means apps that uses UIImagePickerController) will not run on simulator because it need camera which is not available on simulator, here is the final output:

 


Don't want to miss updates? Please click the below button!

Follow MindStick