CodeScanner icon indicating copy to clipboard operation
CodeScanner copied to clipboard

Unreliable torch

Open nitricware opened this issue 3 years ago • 2 comments

Tested on an iPhone 11 Pro running iOS 16;

With isTorchOn set to true the torch can show three behaviours - none of which is reliable:

  1. The torch will not light up
  2. The torch will light up for a brief moment and then be off again.
  3. The torch will not go out when the View is dismissed.

The View is presented in a Sheet:

.sheet(isPresented: $isShowingScanner) {
    CodeScannerView(
        codeTypes: [.code39],
        simulatedData: "04 ASO SCANTEST123",
        isTorchOn: true,
        completion: handleScan)
}

nitricware avatar Sep 13 '22 12:09 nitricware

Can you provide a project (for example a zip archive) to reproduce this issue? I tried in my app and it is not happening...

nathanfallet avatar Sep 20 '22 12:09 nathanfallet

Yes, it happens in this project: https://github.com/nitricware/pSTaRT

nitricware avatar Sep 20 '22 12:09 nitricware

I was experiencing this issue as well. This seems to be related to these stack overflow questions:

However, this only happened when I tried to access my own AVCaptureDevice and enabling the torch myself. @nitricware question made me discover the isTorchOn parameter for CodeScannerView, and after I tried using it I saw the torch working as expected.

I don't know much more about this bug, I'm sorry 😕

ClementCardonnel avatar Feb 02 '23 10:02 ClementCardonnel

Maybe related to #70

nathanfallet avatar Feb 15 '23 10:02 nathanfallet

I think the problem boils down to these LOCs here

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

and here

if let backCamera = AVCaptureDevice.default(for: AVMediaType.video),

So the quickfix is to use the exact same AVCaptureDevice on both lines, the proper fix would be to pass explicitly set AVCaptureDevice to the updateViewController-function, I think.

marckrenn avatar Mar 17 '23 13:03 marckrenn

I changed my code, so that the CodeScannerView gets passed a @State and the torch is off initially. I also implemented a button as suggested in #70 but when I press the button the torch switches on as expected, however the camera freezes.

When I touch the button again, the torch switches off and the camera comes back to life.

Edit: I think i fixed it with #111

nitricware avatar Jul 02 '23 13:07 nitricware