Custom camera view and tack picture in swift

Use following variable

    @IBOutlet var btnCapture: UIButton!
    @IBOutlet var viewCamera: UIView!
    @IBOutlet var btnFlash: UIButton!
    @IBOutlet var btnCameraMode: UIButton!
    
    var stillImageOutput: AVCaptureStillImageOutput!
    
    var videoDeviceInput: AVCaptureDeviceInput?
    var device : AVCaptureDevice!

Use following method for display camera preview in your screen


func ShowCameraPreview() {
        let session = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetPhoto
        let captureVideoPreviewLayer : AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
        captureVideoPreviewLayer.frame = self.viewCamera.bounds
        captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.viewCamera.layer.addSublayer(captureVideoPreviewLayer)
        if btnCameraMode.isSelected == false{
            device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        }else{
            let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
            for device1 in videoDevices!{
                if (device1 as AnyObject).position == AVCaptureDevicePosition.front {
                    device = device1 as! AVCaptureDevice
                    break
                }
            }
        }
        let error: Error? = nil
        videoDeviceInput = try? AVCaptureDeviceInput(device: device)
        if videoDeviceInput == nil {
            print("ERROR: trying to open camera: \(error)")
        }
        session.addInput(videoDeviceInput)
        session.startRunning()
        stillImageOutput = AVCaptureStillImageOutput()
        let outputSettings: [AnyHashable: Any] = [AVVideoCodecKey : AVVideoCodecJPEG]
        stillImageOutput.outputSettings = outputSettings
        session.addOutput(stillImageOutput)
    }

Capture photo


 @IBAction func btnCapture_click(_ sender: UIButton) {
        var videoConnection: AVCaptureConnection? = nil
        
        for connection in stillImageOutput.connections {
            for port in (connection as AnyObject).inputPorts! {
                if (port as AnyObject).mediaType.isEqual(AVMediaTypeVideo) {
                    videoConnection = connection as? AVCaptureConnection
                    break
                }
            }
            if videoConnection != nil { break }
        }
        print("about to request a capture from: \(stillImageOutput)")
    
        stillImageOutput.captureStillImageAsynchronously(from: videoConnection) { (imageSampleBuffer, error) in
            
            let imageData: Data? = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
            let image = UIImage(data: imageData!)
            var imageToDisplay = UIImage()
            if (self.btnCameraMode.isSelected == true){ //Front camera
                 imageToDisplay = UIImage(cgImage: image!.cgImage!, scale: image!.scale, orientation: .leftMirrored)
            }else{ //Back camera
                imageToDisplay = image!
            }
            finalImage = imageToDisplay
            self.goToImageEditor()
        }
    }

Flash on/off functionality


@IBAction func btnFlash_click(_ sender: UIButton){
        if btnFlash.isSelected == true{
            btnFlash.isSelected = false
            self.flashOff(device: device)
        }else{
            btnFlash.isSelected = true
            self.flashOn(device: device)
        }
    }
    
    private func flashOn(device:AVCaptureDevice)
    {
        do{
            if (device.hasTorch)
            {
                try device.lockForConfiguration()
               // device.torchMode = .on
                device.flashMode = .on
                device.unlockForConfiguration()
            }
        }catch{
            print("Device tourch Flash Error ");
        }
    }
    
    private func flashOff(device:AVCaptureDevice)
    {
        do{
            if (device.hasTorch){
                try device.lockForConfiguration()
               // device.torchMode = .off
                device.flashMode = .off
                device.unlockForConfiguration()
            }
        }catch{
            print("Device tourch Flash Error ");
        }
    }

change camera mode


@IBAction func btnCameraMode_click(_ sender: UIButton){
        if btnCameraMode.isSelected == true{
            btnCameraMode.isSelected = false
            self.ShowCameraPreview()
        }else{
            btnCameraMode.isSelected = true
            self.ShowCameraPreview()
        }
    }