Using the camera

Asked

Viewed 193 times

0

I am implementing a screen that I would like to show the image of the camera, but I would just like to show the image, not with features of switching to front camera, cancel, and capture. Is there any way to do that?

So far I could only show with the features, but I just want to show the image where the camera is pointed, without any functionality. I currently use the UIImagePickerController but searching also found tutorials that show with the AVFoundation, if you can enlighten me as to the best of them to do this and how it would 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)
  • 1

    At the end you do not want to take picture? From what I understand you want to take the video from the camera and show it in a UIView, right? If the goal is this you can use the AVCaptureSession of AVFoundation even.

  • That, exactly.

1 answer

0

To show only the "image" (what the camera is seeing), use the 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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.