2

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

neilaur
  • 71
  • 6
  • hmm... Vision is not the "old way", and DataScannerViewController is just a convenience on top of Vision and AVFoundation (so it's easier for people who don't have anything to implement it). So if you already have something implemented with Vision directly., it makes 0 sense for you to use DataScannerViewController – timbre timbre Jan 16 '23 at 00:42
  • It's make sense for sustainability (less code, readability, async/await, Actors), this is why I said "old way" ;-) – neilaur Jan 16 '23 at 01:02
  • Not really, unless you have a plain vanilla use case with no customization (which is clearly not the case). Vision is around for what... 8 years or so? how it's not sustainable? And nothing prevents you from properly using async/await or actors with vision. – timbre timbre Jan 17 '23 at 02:43
  • @neilaur does this code not lock up your AV session? when using this code, I can no longer scan. – hallux Mar 16 '23 at 17:29
  • I have the same problem (on recent phones) and I did not find a solution. If you find something, i'm interesting. – neilaur Mar 17 '23 at 18:09
  • Also with the same problem here, if anyone knows, please help! – Laura Corssac Jul 11 '23 at 10:45
  • I ran into the same issue today when testing on an iPhone. The interesting thing is that when I tested on an iPad the torch came on and off without freezing the DataScannerViewController. They both run the same SwiftUI code with no differences in how the sheet is handled in iOS or iPad OS. I’ll do some more testing tomorrow and see if I can reproduce in a sample app. – MikeMilzz Aug 01 '23 at 00:46

1 Answers1

0

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)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
MikeMilzz
  • 79
  • 8