twostraws / CodeScanner

A SwiftUI view that is able to scan barcodes, QR codes, and more, and send back what was found.
MIT License
1.01k stars 286 forks source link

Add a view modifier to control torch light #50

Closed nathanfallet closed 2 years ago

nathanfallet commented 2 years ago

Adding an ability to turn on/off torch light.

This snippet can be used:

    func torch(torchIsOn: Bool) -> CodeScannerView {
        if let backCamera = AVCaptureDevice.default(for: AVMediaType.video) {
            if backCamera.hasTorch {
                try? backCamera.lockForConfiguration()
                backCamera.torchMode = torchIsOn ? .on : .off
                backCamera.unlockForConfiguration()
            }
        }
        return self
    }

Note: if I have some free time next week, I will try to make a Pull Request for that