Skip to content

Commit 541ea43

Browse files
committed
fix: #85 unreliable torch
Fixes an issue where the CaptureSession became unresponsive while the torch was on or where the CaptureSession wouldn't properly start at all when the torch was on by default.
1 parent f14c3b1 commit 541ea43

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,14 @@ extension CodeScannerView {
416416
#endif
417417

418418
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()
419+
guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else {
420+
return
421+
}
422+
423+
if videoCaptureDevice.hasTorch {
424+
try? videoCaptureDevice.lockForConfiguration()
425+
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
426+
videoCaptureDevice.unlockForConfiguration()
425427
}
426428

427429
if isGalleryPresented && !isGalleryShowing {

0 commit comments

Comments
 (0)