Create Camera View & Taking Photos

Introduction

In this article, we will learn how to access the camera and Photo Library in iOS app development. This is the key feature and a very important one because the clients often require these features in their app. If you are familiar with Android app development, then you can easily access the camera because it is similar in both systems. But, if you are not familiar with Android, follow my article and concentrate on each step. It’s not too hard. So, let’s get started.

Step 1

Open the Xcode (IDE) and create a new project. Give it the name as “CameraApp” and press Next. Now, choose the Single View Application and set the application type to universal. Then, finish creating the project. After that, drag two buttons and one image View from the object library and drop it into the Main Storyboard file. Make the interface like the following.

Open the Xcode

Step 2

Now, at the top position in the right side, you see the Assistant editor option. Click on this. Now, your coding screen break into two parts. Next, we make the outlet of two buttons and one image View, like the following:

code

Step 3

Now, you make action of two buttons, known as Photo Library and Camera. Remember that the action is created after the didRecieveMemory function. Also, remember that you need to select the connection type action not outlet.

action

Step 4

Now, you have two outlets. One is Photo Library and another one is camera. At the top, you can see the ViewController class inherits the UIViewController class. Now, you can declare two more classes after UIViewController. I also have given the code of two buttons. Paste this code into outlet action and your project is ready.

Code

  1. //  
  2. // ViewController.swift  
  3. // Camera App  
  4. //  
  5. // Created by Khawar Islam on 23/07/2016.  
  6. // Copyright © 2016 Khawar Islam. All rights reserved.  
  7. //  
  8. import UIKit  
  9. class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate  
  10. {  
  11.     @IBOutlet weak  
  12.     var PhotoLibaray: UIButton!@IBOutlet weak  
  13.     var ImageDisplay: UIImageView!@IBOutlet weak  
  14.     var cameraa: UIButton!override func viewDidLoad() {  
  15.         super.viewDidLoad()  
  16.             // Do any additional setup after loading the view, typically from a nib.  
  17.     }  
  18.     override func didReceiveMemoryWarning() {  
  19.         super.didReceiveMemoryWarning()  
  20.             // Dispose of any resources that can be recreated.  
  21.     }  
  22.     @IBAction func PhotoLibraryAction(sender: UIButton) {  
  23.         let picker = UIImagePickerController()  
  24.         picker.delegate = self  
  25.         picker.sourceType = .PhotoLibrary  
  26.         presentViewController(picker, animated: true, completion: nil)  
  27.     }  
  28.     @IBAction func CameraAction(sender: UIButton) {  
  29.         let picker = UIImagePickerController()  
  30.         picker.delegate = self  
  31.         picker.sourceType = .Camera  
  32.         presentViewController(picker, animated: true, completion: nil)  
  33.     }  
  34.     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: AnyObject]) {  
  35.         ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as ? UIImage;  
  36.         dismissViewControllerAnimated(true, completion: nil)  
  37.     }  
  38. }  
Last Step

Now, run the app. You see that when you click the Photo library, it gives you the option Allow. Now, the mobile library is open. You can't see anything but when you click on camera, the app fails because we have no real device for checking.

output


Similar Articles