From 52d1395bdf8c8d6078aefcaf78fae97de49f7f44 Mon Sep 17 00:00:00 2001 From: Ralf Ebert Date: Wed, 26 Feb 2025 09:55:27 +0100 Subject: [PATCH] Correctly update the video orientation after the permission prompt appeared - When the permission prompt appears, updateOrientation was called before the AV connection was established and not updated afterwards - Update previewLayer.connection instead of captureSession?.connections.last - Remove implicit forced unwrap for previewLayer property --- Sources/CodeScanner/ScannerViewController.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/CodeScanner/ScannerViewController.swift b/Sources/CodeScanner/ScannerViewController.swift index 292194a..4f6275c 100644 --- a/Sources/CodeScanner/ScannerViewController.swift +++ b/Sources/CodeScanner/ScannerViewController.swift @@ -103,7 +103,7 @@ extension CodeScannerView { #else var captureSession: AVCaptureSession? - var previewLayer: AVCaptureVideoPreviewLayer! + var previewLayer: AVCaptureVideoPreviewLayer? private lazy var viewFinder: UIImageView? = { guard let image = UIImage(named: "viewfinder", in: .module, with: nil) else { @@ -148,7 +148,7 @@ extension CodeScannerView { @objc func updateOrientation() { guard let orientation = view.window?.windowScene?.interfaceOrientation else { return } - guard let connection = captureSession?.connections.last, connection.isVideoOrientationSupported else { return } + guard let previewLayer, let connection = previewLayer.connection, connection.isVideoOrientationSupported else { return } switch orientation { case .portrait: connection.videoOrientation = .portrait @@ -183,8 +183,10 @@ extension CodeScannerView { previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) } + let previewLayer = self.previewLayer! previewLayer.frame = view.layer.bounds previewLayer.videoGravity = .resizeAspectFill + updateOrientation() view.layer.addSublayer(previewLayer) addViewFinder()