iOS (iPhone, iPad) Tutorial on iOS Camera Management

camera is one of the common features in a mobile device. it is possible for us to take pictures with the camera and use it in our application and it is quite simple too.

camera management – steps involved

step 1 − create a simple view based application.

step 2 − add a button in viewcontroller.xib and create ibaction for the button.

step 3 − add an image view and create iboutlet naming it as imageview.

step 4 − update viewcontroller.h as follows −

#import <uikit/uikit.h>

@interface viewcontroller : uiviewcontroller<uiimagepickercontrollerdelegate> {
   uiimagepickercontroller *imagepicker;
   iboutlet uiimageview *imageview;
}

- (ibaction)showcamera:(id)sender;
@end

step 5 − update viewcontroller.m as follows −

#import "viewcontroller.h"

@interface viewcontroller ()
@end

@implementation viewcontroller

- (void)viewdidload {
   [super viewdidload];
}

- (void)didreceivememorywarning {
   [super didreceivememorywarning];
   // dispose of any resources that can be recreated.
}

- (ibaction)showcamera:(id)sender {
   imagepicker.allowsediting = yes;
   
   if ([uiimagepickercontroller issourcetypeavailable:
   uiimagepickercontrollersourcetypecamera]) {
      imagepicker.sourcetype = uiimagepickercontrollersourcetypecamera;
   } else {
      imagepicker.sourcetype = 
      uiimagepickercontrollersourcetypephotolibrary;
   }
   [self presentmodalviewcontroller:imagepicker animated:yes];
}

-(void)imagepickercontroller:(uiimagepickercontroller *)picker 
   didfinishpickingmediawithinfo:(nsdictionary *)info {
      uiimage *image = [info objectforkey:uiimagepickercontrollereditedimage];
      
      if (image == nil) {
         image = [info objectforkey:uiimagepickercontrolleroriginalimage];
      }
   imageview.image = image;
}

-(void)imagepickercontrollerdidcancel:(uiimagepickercontroller *)picker {
   [self dismissmodalviewcontrolleranimated:yes];
}
@end

output

when we run the application and click show camera button, we'll get the following output −

ios tutorial

once we take a picture, we can edit the picture, i.e., move and scale as shown below −

ios tutorial