How can i Take a Picture and Preview it

1.2k Views Asked by At

I recently added a cameraView to my project and everything works well. Although right now i don't really capture photos yet and its what i want to implement next...

it would be great if you could show me how could i implement capturing a photo and previewing it with the code i have

so TakePhoto function will capture the photo and then the image iv taken will be shown to me in the previewPhotoViewController

CustomCameraView :

import UIKit
import AVFoundation


class CustomCameraViewController: UIViewController,UIImagePickerControllerDelegate {
    
    @IBOutlet weak var cameraView: UIView!
    
    let session: AVCaptureSession = AVCaptureSession()
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        session.sessionPreset = AVCaptureSessionPresetHigh
        
        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) {
            do {
                try session.addInput(AVCaptureDeviceInput(device: device))
                
            } catch {
                print(error.localizedDescription)
            }
            
            guard let previewLayer = AVCaptureVideoPreviewLayer(session: session) else {
                print("no preview layer")
                return
            }
            
            self.cameraView.layer.addSublayer(previewLayer)
            previewLayer.frame = self.view.layer.bounds
            
        }
        
        
        session.startRunning()
        
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
 }
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }
    
    func TakePhoto(_ sender: UIButton) {
        
        //Wants to take photo with this button funcntion...
        
        
    }

and i started with the preview page as well :

PreviewPhotoViewController :

import UIKit

class PhotoPreviewViewController: UIViewController {
    
    @IBOutlet weak var imageView: UIImageView!
    
    var takenPhoto :UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        if let availableImage = takenPhoto {
            imageView.image = availableImage
        }
}
    @IBAction func dismissView(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

thank you for your help

1

There are 1 best solutions below

2
On

For AVCaptureSession, you can take a look at this. I've just created a sample of using AVCaptureSession which is simplification of AVCam Apple sample.

Remember to set Privacy - Camera Usage Description in your .plist.


As there are some bugs making AVCaptureSession crash in iOS10 simulator, from this. I prefer to use UIImagePickerController. You can check out the following example:

import UIKit
import AVFoundation
import MobileCoreServices

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func takePhoto(_ sender: UIButton) {
        self.presentPhotoCamera(canEdit: false)
    }

    func presentPhotoCamera(canEdit: Bool) {
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            return
        }

        let type = kUTTypeImage as String
        let imagePicker = UIImagePickerController()

        if UIImagePickerController.isSourceTypeAvailable(.camera) {

            if let availableTypes = UIImagePickerController.availableMediaTypes(for: .camera) {

                if availableTypes.contains(type) {

                    imagePicker.mediaTypes = [type]
                    imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                }
            }

            if UIImagePickerController.isCameraDeviceAvailable(.rear) {
                imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.rear
            } else if UIImagePickerController.isCameraDeviceAvailable(.front) {
                imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
            }
        } else { return }

        imagePicker.allowsEditing = canEdit
        imagePicker.showsCameraControls = true
        imagePicker.delegate = self
        DispatchQueue.main.async {
            self.present(imagePicker, animated: true, completion: nil)
        }
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage

        picker.dismiss(animated: true, completion: {
            self.imageView.image = image
        })
    }
}