From 5caeaa73fb3ec202e89385f79cacabf8d2d43a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Mina=CC=81rik?= Date: Thu, 6 Apr 2017 14:48:47 +0200 Subject: [PATCH] Added support for capture in landscape orientation --- Source/SwiftyCamViewController.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Source/SwiftyCamViewController.swift b/Source/SwiftyCamViewController.swift index 7aa7c17..98ae406 100644 --- a/Source/SwiftyCamViewController.swift +++ b/Source/SwiftyCamViewController.swift @@ -288,6 +288,12 @@ open class SwiftyCamViewController: UIViewController { // Begin Session self.session.startRunning() self.isSessionRunning = self.session.isRunning + + // Preview layer video orientation can be set only after the connection is created + DispatchQueue.main.async { + self.previewLayer.videoPreviewLayer.connection?.videoOrientation = self.getPreviewLayerOrientation() + } + case .notAuthorized: // Prompt to App Settings self.promptToAppSettings() @@ -676,6 +682,20 @@ open class SwiftyCamViewController: UIViewController { self.deviceOrientation = UIDevice.current.orientation } } + + fileprivate func getPreviewLayerOrientation() -> AVCaptureVideoOrientation { + // Depends on layout orientation, not device orientation + switch UIApplication.shared.statusBarOrientation { + case .portrait, .unknown: + return AVCaptureVideoOrientation.portrait + case .landscapeLeft: + return AVCaptureVideoOrientation.landscapeLeft + case .landscapeRight: + return AVCaptureVideoOrientation.landscapeRight + case .portraitUpsideDown: + return AVCaptureVideoOrientation.portraitUpsideDown + } + } fileprivate func getVideoOrientation() -> AVCaptureVideoOrientation { guard shouldUseDeviceOrientation, let deviceOrientation = self.deviceOrientation else { return previewLayer!.videoPreviewLayer.connection.videoOrientation }