Skip to content

Commit

Permalink
Merge pull request #111 from nitricware/torchFix
Browse files Browse the repository at this point in the history
fix: #85 unreliable torch
  • Loading branch information
nathanfallet authored Jul 10, 2023
2 parents f14c3b1 + e6f2844 commit bf5d708
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Sources/CodeScanner/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extension CodeScannerView {
var lastTime = Date(timeIntervalSince1970: 0)
private let showViewfinder: Bool

let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)

private var isGalleryShowing: Bool = false {
didSet {
// Update binding
Expand Down Expand Up @@ -145,7 +147,6 @@ extension CodeScannerView {

var captureSession: AVCaptureSession?
var previewLayer: AVCaptureVideoPreviewLayer!
let fallbackVideoCaptureDevice = AVCaptureDevice.default(for: .video)

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

func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
if let backCamera = AVCaptureDevice.default(for: AVMediaType.video),
backCamera.hasTorch
{
try? backCamera.lockForConfiguration()
backCamera.torchMode = isTorchOn ? .on : .off
backCamera.unlockForConfiguration()
guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else {
return
}

if videoCaptureDevice.hasTorch {
try? videoCaptureDevice.lockForConfiguration()
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
videoCaptureDevice.unlockForConfiguration()
}

if isGalleryPresented && !isGalleryShowing {
Expand Down

0 comments on commit bf5d708

Please sign in to comment.