yannickl / QRCodeReader.swift

Simple QRCode reader in Swift
MIT License
1.33k stars 330 forks source link

Flash doesn't work in Scan in preview option #151

Closed j8055 closed 6 years ago

j8055 commented 6 years ago

Hi, I noticed that the flash wasn't working when I used the scan in preview from the example

j8055 commented 6 years ago

Never mind Figured it out 👍

Sopheak0 commented 5 years ago

hi! how is your solution? i meet the same problem

j8055 commented 5 years ago

Hi,

What I did was add a target for the toggle torch button like this

qrCodeReaderView?.toggleTorchButton?.addTarget(self, action: #selector(toggleTorch), for: .touchUpInside)

And then used my own method for flash

extension QRCodeScanViewController {
    @objc func toggleTorch() {

        isTorchLightOn = !isTorchLightOn

        guard let device = AVCaptureDevice.default(for: AVMediaType.video)
            else {return}

        if device.hasTorch {
            do {
                try device.lockForConfiguration()

                if isTorchLightOn == true {
                    device.torchMode = .on
                } else {
                    device.torchMode = .off
                }

                device.unlockForConfiguration()
            } catch {
                print("Torch could not be used")
            }
        } else {
            print("Torch is not available")
        }
    }
}