fix: Fix orientation glitching on first frame

This commit is contained in:
Marc Rousavy 2022-11-25 15:39:04 +01:00
parent a65b8720bd
commit bf71901968

View File

@ -10,18 +10,9 @@ import Foundation
import UIKit
extension CameraView {
/// Returns the current _interface_ orientation of the main window
private var windowInterfaceOrientation: UIInterfaceOrientation {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? .unknown
} else {
return UIApplication.shared.statusBarOrientation
}
}
/// Orientation of the input connection (preview)
private var inputOrientation: UIInterfaceOrientation {
return windowInterfaceOrientation
return .portrait
}
// Orientation of the output connections (photo, video, frame processor)
@ -37,25 +28,18 @@ extension CameraView {
}
internal func updateOrientation() {
// Updates the Orientation for all rotable connections (outputs) as well as for the preview layer
DispatchQueue.main.async {
// `windowInterfaceOrientation` and `videoPreviewLayer` should only be accessed from UI thread
let isMirrored = self.videoDeviceInput?.device.position == .front
// Updates the Orientation for all rotable
let isMirrored = self.videoDeviceInput?.device.position == .front
self.videoPreviewLayer.connection?.setInterfaceOrientation(self.inputOrientation)
let connectionOrientation = self.outputOrientation
self.cameraQueue.async {
// Run those updates on cameraQueue since they can be blocking.
self.captureSession.outputs.forEach { output in
output.connections.forEach { connection in
if connection.isVideoMirroringSupported {
connection.automaticallyAdjustsVideoMirroring = false
connection.isVideoMirrored = isMirrored
}
connection.setInterfaceOrientation(connectionOrientation)
}
let connectionOrientation = self.outputOrientation
// Run those updates on cameraQueue since they can be blocking.
self.captureSession.outputs.forEach { output in
output.connections.forEach { connection in
if connection.isVideoMirroringSupported {
connection.automaticallyAdjustsVideoMirroring = false
connection.isVideoMirrored = isMirrored
}
connection.setInterfaceOrientation(connectionOrientation)
}
}
}