Enable/Disable torch with DataScannerViewController

402 Views Asked by At

On iOS16, I try to use the DataScannerViewController (for barcode reading) to remplace old way with Vision but when I enable the torch, DataScannerViewController is freezing...

The code to enable/disable torch :

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

do {
    try device.lockForConfiguration()
    device.torchMode = device.isTorchActive ? .off : .on
    device.unlockForConfiguration()
} catch {
    print(error)
}

(This code is working well with Vision)

Thanks a lot

1

There are 1 best solutions below

2
On

I followed the example on this site to configure the full AVCaptureDevice.DiscoverySession and then use that to activate the torch. So far the issue seems to be resolved.

let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInTripleCamera, .builtInDualWideCamera, .builtInUltraWideCamera, .builtInWideAngleCamera, .builtInTrueDepthCamera], mediaType: AVMediaType.video, position: cameraPosition)