Added support for device auto rotation

This commit is contained in:
Andrew Walz 2017-05-29 20:55:59 -06:00
parent fa9e75fe46
commit 151bed4623
2 changed files with 45 additions and 5 deletions

View file

@ -27,7 +27,7 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
cameraDelegate = self
maximumVideoDuration = 10.0
shouldUseDeviceOrientation = true
allowAutoRotate = true
allowAutoRotate = false
addButtons()
}

View file

@ -281,14 +281,54 @@ open class SwiftyCamViewController: UIViewController {
// MARK: ViewDidLayoutSubviews
/// ViewDidLayoutSubviews() Implementation
/// ViewDidLayoutSubviews() Implementation
private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
layer.videoOrientation = orientation
previewLayer.frame = self.view.bounds
}
/*override open func viewDidLayoutSubviews() {
previewLayer.frame = CGRect(x: 0.0, y: 0.0, width: view.bounds.width, height: view.bounds.height)
override open func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
} */
if let connection = self.previewLayer?.videoPreviewLayer.connection {
let currentDevice: UIDevice = UIDevice.current
let orientation: UIDeviceOrientation = currentDevice.orientation
let previewLayerConnection : AVCaptureConnection = connection
if previewLayerConnection.isVideoOrientationSupported {
switch (orientation) {
case .portrait: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
case .landscapeRight: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)
break
case .landscapeLeft: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)
break
case .portraitUpsideDown: updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)
break
default: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
}
}
}
}
// MARK: ViewDidAppear
/// ViewDidAppear(_ animated:) Implementation