Skip to content

Commit f855d6d

Browse files
committed
introduce rotationCoordinator in code scanner vc
1 parent 620b124 commit f855d6d

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

WooCommerce/Classes/ViewRelated/Products/Scanner/CodeScannerViewController.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AVFoundation
2+
import Combine
23
import UIKit
34
import Vision
45

@@ -36,6 +37,8 @@ final class CodeScannerViewController: UIViewController {
3637
private let sessionQueue = DispatchQueue(label: "qrlogincamerasession.queue.serial")
3738
private let session = AVCaptureSession()
3839
private var requests = [VNRequest]()
40+
private var rotationCoordinator: AVCaptureDevice.RotationCoordinator?
41+
private var cancellables = Set<AnyCancellable>()
3942

4043
private lazy var throttler: Throttler = Throttler(seconds: 0.1)
4144

@@ -187,6 +190,15 @@ private extension CodeScannerViewController {
187190
previewLayer.videoGravity = .resizeAspectFill
188191
previewLayer.frame = videoOutputImageView.bounds
189192
videoOutputImageView.layer.addSublayer(previewLayer)
193+
rotationCoordinator = AVCaptureDevice.RotationCoordinator(device: captureDevice, previewLayer: previewLayer)
194+
195+
rotationCoordinator?.publisher(for: \.videoRotationAngleForHorizonLevelPreview)
196+
.sink { [weak self] angle in
197+
guard let self = self,
198+
let connection = self.previewLayer?.connection else { return }
199+
connection.videoRotationAngle = angle
200+
}
201+
.store(in: &cancellables)
190202

191203
sessionQueue.async { [weak self] in
192204
self?.session.startRunning()
@@ -290,27 +302,27 @@ private extension CodeScannerViewController {
290302
//
291303
private extension CodeScannerViewController {
292304
func updatePreviewLayerOrientation() {
293-
if let connection = previewLayer?.connection, connection.isVideoOrientationSupported {
305+
if let connection = previewLayer?.connection {
294306
let orientation = view.window?.windowScene?.interfaceOrientation
295-
let videoOrientation: AVCaptureVideoOrientation
307+
let videoRotationAngle: CGFloat
296308
switch orientation {
297309
case .portrait:
298-
videoOrientation = .portrait
310+
videoRotationAngle = 90
299311
case .landscapeRight:
300-
videoOrientation = .landscapeRight
312+
videoRotationAngle = 0
301313
case .landscapeLeft:
302-
videoOrientation = .landscapeLeft
314+
videoRotationAngle = 180
303315
case .portraitUpsideDown:
304-
videoOrientation = .portraitUpsideDown
316+
videoRotationAngle = 270
305317
default:
306-
videoOrientation = .portrait
318+
videoRotationAngle = 90
307319
}
308-
updatePreviewLayerVideoOrientation(connection: connection, orientation: videoOrientation)
320+
updatePreviewLayerVideoRotationAngle(connection: connection, angle: videoRotationAngle)
309321
}
310322
}
311323

312-
func updatePreviewLayerVideoOrientation(connection: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
313-
connection.videoOrientation = orientation
324+
func updatePreviewLayerVideoRotationAngle(connection: AVCaptureConnection, angle: CGFloat) {
325+
connection.videoRotationAngle = angle
314326
}
315327
}
316328

0 commit comments

Comments
 (0)