Skip to content

Instantly share code, notes, and snippets.

@thomaspockrandt
Created October 28, 2017 02:53
Show Gist options
  • Save thomaspockrandt/d4186e7a7a9884786d5070e617bf7007 to your computer and use it in GitHub Desktop.
Save thomaspockrandt/d4186e7a7a9884786d5070e617bf7007 to your computer and use it in GitHub Desktop.
CaptureViewController
import UIKit
import AVFoundation
class CaptureViewController: UIViewController {
var captureSession: AVCaptureSession?
var captureVideoPreviewLayer: AVCaptureVideoPreviewLayer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
guard let captureDevice = AVCaptureDevice.default(for: .video) else {
return
}
guard let captureDeviceInput = try? AVCaptureDeviceInput(device: captureDevice) else {
return
}
self.captureSession = AVCaptureSession()
self.captureSession?.addInput(captureDeviceInput)
guard let captureSession = self.captureSession else {
return
}
self.captureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.captureVideoPreviewLayer?.videoGravity = .resizeAspectFill
self.captureVideoPreviewLayer?.frame = view.layer.bounds
guard let captureVideoPreviewLayer = self.captureVideoPreviewLayer else {
return
}
self.view.layer.addSublayer(captureVideoPreviewLayer)
self.captureSession?.startRunning()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment