The iOS library provides the class UIImagePickerController which is the user interface for managing the user interaction with the camera or with the photo library. The UIImagePickerController requires the use of a delegate to respond to interactions.

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name ( Lets say SimpleCameraApplication).
  3. Click next button and the Project will be created.
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “imageView” object and drag it into the view.
  5. Create a IBoutlet for the “imageView” in the ViewContoller.h.

6.  In” ViewController.h”  just write these delegate methods: <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UIAlertViewDelegate>

 

After Connectimg the IBOutlet and writing the delegates Your View Controller will Look something Like this

//

// ViewController.h

// SimpleCameraApplication

//

// Created by Nilesh on 4/13/16.

// Copyright © 2016 Nilesh. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,

UIActionSheetDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *chosenImage;

@end

7. Now go to “MainStoryBoard” again and take a button from the object library and drag it in the View. Create a action method for the button in the “ViewController.m” and then we will write the action method for it.  Basically we are making a object of alertcontroller and giving the user three option TakePhoto, Choose Photo and cancel.

Note- UIalertController  class is available after ios8, If you want to support for ios7 then use UiAlertView .

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

[actionSheet addAction:[UIAlertAction actionWithTitle:@”Cancel” style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

// Cancel button tappped do nothing.

}]];

[actionSheet addAction:[UIAlertAction actionWithTitle:@”Take photo” style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

// take photo button tapped.

[self takePhoto];

}]];

[actionSheet addAction:[UIAlertAction actionWithTitle:@”Choose photo” style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

// choose photo button tapped.

[self choosePhoto];

}]];

[self presentViewController:actionSheet animated:YES completion:nil];

}

8. Now we wiil define the custom Methods takePhoto and choosePhoto.  In takePhoto method we will be making object of UIImagePickerController and then selecting the sourceType as Camera. In choose photo we will be doing the same thing but we will keep the source type as Gallery.

Note: If you are running on simulator then You cannot take photo as camera is not present so application will crash. So try only on Device this option. Your methods will look loke this.

#pragma mark – Custom Methods

-(void)takePhoto{

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

picker.delegate= self;

picker.allowsEditing = YES;

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

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

}

-(void)choosePhoto{

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

picker.delegate= self;

picker.allowsEditing = YES;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

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

}

9. Now write the delegate method of UIImagePickerController for getting the image. This method will be called once the image has been taken from camera or selected from the gallery.

#pragma mark – Camera Delegate Methods

– (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

self.chosenImage.image = info[UIImagePickerControllerEditedImage];

[picker dismissViewControllerAnimated:YES completion:NULL];

}

10. Run the Project and see the desired output. Enjoy Coding 🙂

 

Advertisements