Camera and UIImagePickerController to take photos with swift

You can easily integrate camera functionality in your application using UIImagePickerController which is built-in UIKit. It requires very few steps to do so. I am providing the basic steps below

Make your viewcontroller calss conform to UIImagePickerControllerDelegate and UINavigationControllerDelegate

//
//  ImagePickerViewController.swift
//  ImagePicker
//
//  Created by amarendra on 20/06/20.
//  Copyright © 2020 amarendra. All rights reserved.
//

import UIKit

class ImagePickerViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

}

Add extention to your viewcontroller

extension ImagePickerViewController{
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        guard let image = info[.editedImage] as? UIImage else {
               print("Error picking image")
               return
           }
        //use your image
        
    }
}

Your code is almost done. Now add some button or function from where you will launch picker. And present your picker

@IBAction func cameraTapped(_ sender: UIButton) {
  //launch camera
        let cameraView = UIImagePickerController()
        cameraView.sourceType = .camera
        cameraView.allowsEditing = true
        cameraView.delegate = self
        present(cameraView, animated: true)
}

Now when you will click on the camera button it will call the cameraTapped function which in turn will launch the native camera . NOTE: in order to test on simulator set

    cameraView.sourceType = .photoLibrary

Thanks for reading. Comment if you face any issue

A pat on the back !!