|
// |
|
// ViewController.swift |
|
// nouicam |
|
// |
|
// Created by Ulrich Petri on 27.04.2017. |
|
// Copyright © 2017 Ulrich Petri. |
|
// |
|
// License: MIT |
|
// |
|
|
|
import UIKit |
|
import AVFoundation |
|
|
|
extension AVCaptureVideoOrientation { |
|
var uiInterfaceOrientation: UIInterfaceOrientation { |
|
get { |
|
switch self { |
|
case .landscapeLeft: return .landscapeLeft |
|
case .landscapeRight: return .landscapeRight |
|
case .portrait: return .portrait |
|
case .portraitUpsideDown: return .portraitUpsideDown |
|
} |
|
} |
|
} |
|
|
|
init(ui:UIInterfaceOrientation) { |
|
switch ui { |
|
case .landscapeRight: self = .landscapeRight |
|
case .landscapeLeft: self = .landscapeLeft |
|
case .portrait: self = .portrait |
|
case .portraitUpsideDown: self = .portraitUpsideDown |
|
default: self = .portrait |
|
} |
|
} |
|
|
|
init?(orientation:UIDeviceOrientation) { |
|
switch orientation { |
|
case .landscapeRight: self = .landscapeLeft |
|
case .landscapeLeft: self = .landscapeRight |
|
case .portrait: self = .portrait |
|
case .portraitUpsideDown: self = .portraitUpsideDown |
|
default: |
|
return nil |
|
} |
|
} |
|
} |
|
|
|
class ViewController: UIViewController { |
|
@IBOutlet weak var previewView: UIView! |
|
var session: AVCaptureSession? |
|
var videoPreviewLayer: AVCaptureVideoPreviewLayer? |
|
|
|
override func viewWillAppear(_ animated: Bool) { |
|
super.viewWillAppear(animated) |
|
session = AVCaptureSession() |
|
session!.sessionPreset = AVCaptureSessionPreset1920x1080 |
|
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) |
|
var error: NSError? |
|
var input: AVCaptureDeviceInput! |
|
do { |
|
input = try AVCaptureDeviceInput(device: backCamera) |
|
} catch let error1 as NSError { |
|
error = error1 |
|
input = nil |
|
print(error!.localizedDescription) |
|
} |
|
if error == nil && session!.canAddInput(input) { |
|
session!.addInput(input) |
|
} |
|
let videoOutput = AVCaptureVideoDataOutput() |
|
if session!.canAddOutput(videoOutput) { |
|
session!.addOutput(videoOutput) |
|
} |
|
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session) |
|
videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect |
|
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait |
|
previewView.layer.addSublayer(videoPreviewLayer!) |
|
session!.startRunning() |
|
} |
|
|
|
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) { |
|
videoPreviewLayer!.frame = previewView.bounds |
|
} |
|
|
|
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) { |
|
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation(ui: toInterfaceOrientation) |
|
} |
|
|
|
override func viewDidAppear(_ animated: Bool) { |
|
super.viewDidAppear(animated) |
|
videoPreviewLayer!.frame = previewView.bounds |
|
} |
|
|
|
override func viewDidLoad() { |
|
super.viewDidLoad() |
|
UIApplication.shared.isIdleTimerDisabled = true |
|
} |
|
|
|
override func didReceiveMemoryWarning() { |
|
super.didReceiveMemoryWarning() |
|
} |
|
|
|
override var prefersStatusBarHidden: Bool { |
|
return true |
|
} |
|
} |
|
|