Javatpoint Logo
Javatpoint Logo

How to capture the image using the iOS app

We can take pictures in the iOS app by using AVFoundation, which is a framework in iOS. However, in this tutorial, we will use UIImagePickerControllerDelegate methods. Here, we will build a simple application that will capture the picture using the device camera and set it to the image view. We will not use AVFoundation in this tutorial. Instead of that, we will use UIImagePickerController.

The image picker controller is mainly used to manage the user interactions and deliver the results of the interactions to a delegate object. The role and appearance of an image picker controller depend on the source type we assign to it before we present it.

Create a project in XCode and add the ImageView to the ViewController in the storyboard.

How to capture the image using the iOS app

Now, Create the outlet in the ViewController class. We need to ensure that our ViewController class conforms to UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols.

Once we conform our view controller to the above-mentioned protocols, create a @objc type function as openCam(), which will contain the business code to open the camera. The declaration of openCam() is given below.

In the ViewController, create a TapGestureRecognizer inside viewDidLoad() so that once we tap on the ViewController, the camera should open.

Now, we will define the openCamera function, which will contain the instance of UIImagePickerController. We need to define the properties, for instance, and present the one.

Once we are done with the above steps, we'll need to implement the didFinishPickingMediaWithInfo method of UIImagePickerControllerDelegate. We will get the image inside the method which is captured by the user that can be set to image outlet which we have created in step 1.

We need to add the camera usage description key in our info.plist and describe why our application wants to use the camera. Add the following code to the info.plist file.

We need to run this app on an actual iOS device since we can't run the same iOS simulator.

The application code is given below.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA