Skip to content

Commit bf5d708

Browse files
authored
Merge pull request #111 from nitricware/torchFix
fix: #85 unreliable torch
2 parents f14c3b1 + e6f2844 commit bf5d708

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ extension CodeScannerView {
2222
var lastTime = Date(timeIntervalSince1970: 0)
2323
private let showViewfinder: Bool
2424

25+
let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)
26+
2527
private var isGalleryShowing: Bool = false {
2628
didSet {
2729
// Update binding
@@ -145,7 +147,6 @@ extension CodeScannerView {
145147

146148
var captureSession: AVCaptureSession?
147149
var previewLayer: AVCaptureVideoPreviewLayer!
148-
let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)
149150

150151
private lazy var viewFinder: UIImageView? = {
151152
guard let image = UIImage(named: "viewfinder", in: .module, with: nil) else {
@@ -416,12 +417,14 @@ extension CodeScannerView {
416417
#endif
417418

418419
func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
419-
if let backCamera = AVCaptureDevice.default(for: AVMediaType.video),
420-
backCamera.hasTorch
421-
{
422-
try? backCamera.lockForConfiguration()
423-
backCamera.torchMode = isTorchOn ? .on : .off
424-
backCamera.unlockForConfiguration()
420+
guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else {
421+
return
422+
}
423+
424+
if videoCaptureDevice.hasTorch {
425+
try? videoCaptureDevice.lockForConfiguration()
426+
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
427+
videoCaptureDevice.unlockForConfiguration()
425428
}
426429

427430
if isGalleryPresented && !isGalleryShowing {

0 commit comments

Comments
 (0)