Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AVFoundation
import Combine
import UIKit
import Vision

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

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

Expand Down Expand Up @@ -160,10 +163,8 @@ private extension CodeScannerViewController {
/// Enables and starts live stream video, if available.
func startLiveVideo() {
session.sessionPreset = .photo
if #available(iOS 16.0, *) {
if session.isMultitaskingCameraAccessSupported {
session.isMultitaskingCameraAccessEnabled = true
}
if session.isMultitaskingCameraAccessSupported {
session.isMultitaskingCameraAccessEnabled = true
}

guard let captureDevice = AVCaptureDevice.default(for: .video),
Expand All @@ -187,6 +188,15 @@ private extension CodeScannerViewController {
previewLayer.videoGravity = .resizeAspectFill
previewLayer.frame = videoOutputImageView.bounds
videoOutputImageView.layer.addSublayer(previewLayer)
rotationCoordinator = AVCaptureDevice.RotationCoordinator(device: captureDevice, previewLayer: previewLayer)

rotationCoordinator?.publisher(for: \.videoRotationAngleForHorizonLevelPreview)
.sink { [weak self] angle in
guard let self = self,
let connection = self.previewLayer?.connection else { return }
connection.videoRotationAngle = angle
}
.store(in: &cancellables)

sessionQueue.async { [weak self] in
self?.session.startRunning()
Expand Down Expand Up @@ -289,28 +299,35 @@ private extension CodeScannerViewController {
// MARK: Orientation Handling
//
private extension CodeScannerViewController {
enum VideoRotationAngle {
static let portrait: CGFloat = 90
static let landscapeRight: CGFloat = 0
static let landscapeLeft: CGFloat = 180
static let portraitUpsideDown: CGFloat = 270
}
Comment on lines +302 to +307
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there is no enum replacement to what we had before iOS17, or I couldn't find it 🤔 I made a new one here


func updatePreviewLayerOrientation() {
if let connection = previewLayer?.connection, connection.isVideoOrientationSupported {
if let connection = previewLayer?.connection {
let orientation = view.window?.windowScene?.interfaceOrientation
let videoOrientation: AVCaptureVideoOrientation
let videoRotationAngle: CGFloat
switch orientation {
case .portrait:
videoOrientation = .portrait
videoRotationAngle = VideoRotationAngle.portrait
case .landscapeRight:
videoOrientation = .landscapeRight
videoRotationAngle = VideoRotationAngle.landscapeRight
case .landscapeLeft:
videoOrientation = .landscapeLeft
videoRotationAngle = VideoRotationAngle.landscapeLeft
case .portraitUpsideDown:
videoOrientation = .portraitUpsideDown
videoRotationAngle = VideoRotationAngle.portraitUpsideDown
default:
videoOrientation = .portrait
videoRotationAngle = VideoRotationAngle.portrait
}
updatePreviewLayerVideoOrientation(connection: connection, orientation: videoOrientation)
updatePreviewLayerVideoRotationAngle(connection: connection, angle: videoRotationAngle)
}
}

func updatePreviewLayerVideoOrientation(connection: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
connection.videoOrientation = orientation
func updatePreviewLayerVideoRotationAngle(connection: AVCaptureConnection, angle: CGFloat) {
connection.videoRotationAngle = angle
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="23727" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23721"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -36,14 +35,14 @@
<rect key="frame" x="0.0" y="0.0" width="414" height="269"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wCb-QS-xTf">
<rect key="frame" x="20" y="20" width="75" height="34.5"/>
<rect key="frame" x="20" y="20" width="75" height="40.5"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Button"/>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uYb-k2-4wm">
<rect key="frame" x="338.5" y="20" width="55.5" height="34"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" image="arrow.triangle.2.circlepath.camera" catalog="system"/>
<buttonConfiguration key="configuration" style="plain" image="arrow.trianglehead.2.clockwise.rotate.90.camera" catalog="system"/>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
Expand Down Expand Up @@ -94,7 +93,7 @@
</view>
</objects>
<resources>
<image name="arrow.triangle.2.circlepath.camera" catalog="system" width="128" height="93"/>
<image name="arrow.trianglehead.2.clockwise.rotate.90.camera" catalog="system" width="128" height="93"/>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
Expand Down