Introduction

In iOS there are two ways to take pictures or record videos

  • Using UIImagePickerController class from UIKit Framework
  • Using AV Foundation framework along with UIKit Framework

Here the first way that is by using  UIImagePickerController .It have its own builtin interface for opening the camera and taking pictures or selecting pictures from gallery we cannot customize its UI and we use it as it is. but if we want our app to use fully customizable interface then we should use the AV Foundation along with UIKit .

In this lecture we will learn only the first method and for the second method you should follow following tutorial. [ABC]

Taking a picture or movie from UIImagePickerController is a three part process.that proceeds as an interplay between our code and the system:

  1. First we instantiate the UIImagePickerController and modally present it on our existing view controller
  2. Then the system manages the camera interface and its interaction. Here user can perform many actions like takes a picture or movie, or just cancel the operation
  3. When user finishes his actions then  system invokes the delegate of UIImagePickerController which then handle the results of the user’s interaction.

Instantiate and configure the Camera interface

Before presenting the camera interface modally the following things must be take under considerations .

  1. The device which you are using for your app must have a Camera
  2. The device Camera must be available for you to use . . . You can test it by isSourceTypeAvailable:  method of   UIImagePickerController Class.
  3. You must have implemented the delegates methods of UIImagePickerController to handle the interaction with Image Picker Controller.

 

After fulfilling the above three prerequisites you will then configure the following option of Image Picker Controller

  • Source type – We have to set the sourceType to UIImagePickerControllerSourceTypeCamera for using actual device
  • Media types– Here we have to specifiy either we want Image or Movie Capture or both by setting mediaTypes to specific Flag kUTTypeImage and kUTTypeMovie or we can give in the form of NSArray. and before setting this property you can check the available mediaTypes using the method availableMediaTypesForSourceType.
  • Editing controls-  This property is used if we want camera interface to provide the editing functionality like scaling image or trimming movie by setting hte allowsEditing property to YES or NO
  • Delegate objects – and then assign the delegate object to image picker controller’s delegate property.

 

In the above code if we only want to capture the Image then we will set the mediaTypes to kUTTypeImage only

and if you want to capture only Movie then you can change from kUTTypeImage to kUTTypeMovie

now if we want to start the camera function then we will just use the following function upon button click

Above  delegate object must conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols.Mean you should extend your main View controller with above two protocols.

Implementing a Delegate for Camera Interface