I have downloaded a simpleCamera view from Cocoa Controls which use this method
- (AVCaptureVideoOrientation)orientationForConnection
{
AVCaptureVideoOri
When you use UIApplication.shared.statusBarOrientation, Xcode 11 will show the warning 'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead.
This answer is in Swift 5, and supports both iOS 13 and lower versions. It assumes the following:
previewLayer of type AVCaptureVideoPreviewLayerFirst, in your SceneDelegate.swift, add the following lines:
static var lastCreatedScene: UIWindowScene?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let newScene = (scene as? UIWindowScene) else { return }
Self.lastCreatedScene = newScene
}
Then in your view, add the following function and call it in layoutSubviews():
func refreshOrientation() {
let videoOrientation: AVCaptureVideoOrientation
let statusBarOrientation: UIInterfaceOrientation
if #available(iOS 13, *) {
statusBarOrientation = SceneDelegate.lastCreatedScene?.interfaceOrientation ?? .portrait
} else {
statusBarOrientation = UIApplication.shared.statusBarOrientation
}
switch statusBarOrientation {
case .unknown:
videoOrientation = .portrait
case .portrait:
videoOrientation = .portrait
case .portraitUpsideDown:
videoOrientation = .portraitUpsideDown
case .landscapeLeft:
videoOrientation = .landscapeLeft
case .landscapeRight:
videoOrientation = .landscapeRight
@unknown default:
videoOrientation = .portrait
}
self.previewLayer.connection?.videoOrientation = videoOrientation
}