Using the camera

0

I am implementing a screen that I would like to show the camera image, but I would like to only show the image, not with front camera switching, canceling, or capturing features. Is there any way to do this?

So far I've only been able to show with the features, but I just want to show the image where the camera is pointing, without any functionality. I currently use UIImagePickerController however searching I also found tutorials that show with AVFoundation , if you can clarify to me the best of them to do this and how would it be possible.

Current code:

var imagePicker: UIImagePickerController = UIImagePickerController()

imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.view.frame = viewCamera.frame
viewCamera.addSubview(imagePicker.view)
    
asked by anonymous 03.03.2015 / 15:24

1 answer

0

To show only the "image" (what the camera is seeing), use AVCaptureVideoPreviewLayer (solution in Swift 3.1 ):

import UIKit
import AVFoundation

class MyViewController : UIViewController {

    @IBOutlet weak var cameraCanvasView: UIView!

    fileprivate var captureSession = AVCaptureSession()
    fileprivate var movieOutput = AVCaptureMovieFileOutput()
    fileprivate var videoLayer : AVCaptureVideoPreviewLayer!

    fileprivate lazy var frontCameraDevice: AVCaptureDevice? = {
        let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as! [AVCaptureDevice]
        return devices.filter { $0.position == .front }.first
    }()

    // Setup
    private func setup() {

        //start session configuration
        captureSession.beginConfiguration()
        captureSession.sessionPreset = AVCaptureSessionPresetHigh

        // layer
        videoLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoLayer.frame = cameraCanvasView.bounds
        cameraCanvasView.layer.addSublayer(videoLayer)

        // add device inputs
        captureSession.addInput(deviceInputFrom(device: frontCameraDevice))

        // start session
        captureSession.commitConfiguration()
        captureSession.startRunning()
    }

    // MARK: - Lifecycle Methods
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setup()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        videoLayer.frame = cameraCanvasView.bounds // aqui você redimensiona o AVCaptureVideoPreviewLayer quando o AutoLayout atua
    }
}
    
31.03.2017 / 18:35