Skip to content

Commit 52d1395

Browse files
committed
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
1 parent 5e88643 commit 52d1395

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ extension CodeScannerView {
103103
#else
104104

105105
var captureSession: AVCaptureSession?
106-
var previewLayer: AVCaptureVideoPreviewLayer!
106+
var previewLayer: AVCaptureVideoPreviewLayer?
107107

108108
private lazy var viewFinder: UIImageView? = {
109109
guard let image = UIImage(named: "viewfinder", in: .module, with: nil) else {
@@ -148,7 +148,7 @@ extension CodeScannerView {
148148

149149
@objc func updateOrientation() {
150150
guard let orientation = view.window?.windowScene?.interfaceOrientation else { return }
151-
guard let connection = captureSession?.connections.last, connection.isVideoOrientationSupported else { return }
151+
guard let previewLayer, let connection = previewLayer.connection, connection.isVideoOrientationSupported else { return }
152152
switch orientation {
153153
case .portrait:
154154
connection.videoOrientation = .portrait
@@ -183,8 +183,10 @@ extension CodeScannerView {
183183
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
184184
}
185185

186+
let previewLayer = self.previewLayer!
186187
previewLayer.frame = view.layer.bounds
187188
previewLayer.videoGravity = .resizeAspectFill
189+
updateOrientation()
188190
view.layer.addSublayer(previewLayer)
189191
addViewFinder()
190192

0 commit comments

Comments
 (0)