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 import UIKit
extension CameraView { 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) /// Orientation of the input connection (preview)
private var inputOrientation: UIInterfaceOrientation { private var inputOrientation: UIInterfaceOrientation {
return windowInterfaceOrientation return .portrait
} }
// Orientation of the output connections (photo, video, frame processor) // Orientation of the output connections (photo, video, frame processor)
@ -37,25 +28,18 @@ extension CameraView {
} }
internal func updateOrientation() { internal func updateOrientation() {
// Updates the Orientation for all rotable connections (outputs) as well as for the preview layer // Updates the Orientation for all rotable
DispatchQueue.main.async { let isMirrored = self.videoDeviceInput?.device.position == .front
// `windowInterfaceOrientation` and `videoPreviewLayer` should only be accessed from UI thread
let isMirrored = self.videoDeviceInput?.device.position == .front
self.videoPreviewLayer.connection?.setInterfaceOrientation(self.inputOrientation) let connectionOrientation = self.outputOrientation
// Run those updates on cameraQueue since they can be blocking.
let connectionOrientation = self.outputOrientation self.captureSession.outputs.forEach { output in
self.cameraQueue.async { output.connections.forEach { connection in
// Run those updates on cameraQueue since they can be blocking. if connection.isVideoMirroringSupported {
self.captureSession.outputs.forEach { output in connection.automaticallyAdjustsVideoMirroring = false
output.connections.forEach { connection in connection.isVideoMirrored = isMirrored
if connection.isVideoMirroringSupported {
connection.automaticallyAdjustsVideoMirroring = false
connection.isVideoMirrored = isMirrored
}
connection.setInterfaceOrientation(connectionOrientation)
}
} }
connection.setInterfaceOrientation(connectionOrientation)
} }
} }
} }