Unreliable torch
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:
- The torch will not light up
- The torch will light up for a brief moment and then be off again.
- 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)
}
Can you provide a project (for example a zip archive) to reproduce this issue? I tried in my app and it is not happening...
Yes, it happens in this project: https://github.com/nitricware/pSTaRT
I was experiencing this issue as well. This seems to be related to these stack overflow questions:
- AVCaptureSession freezes when torch is turned on
- Device torch turns off when starting AVCaptureSession
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 😕
Maybe related to #70
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.
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