1

I am facing issue while turning on torch in iOS 12. It is working fine till iOS 11 but not in iOS 12.

Torch is not turning on. I am using the below code.

if device.hasTorch && device.isTorchModeSupported(torchMode) {
    do {
        try device.lockForConfiguration()
        device.torchMode = torchMode
        device.unlockForConfiguration()

    } 
    catch let error as NSError {
        print(error)
        Utils.showAlertWithTitleInController("No Torch Error", message: error.debugDescription, controller: self)
    }
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
CodeCracker
  • 461
  • 5
  • 17
  • Do you get an error? Did the user granted access to the AV device and did you check that status? Please be more explicit in how far this code gets and what you've tried, instead of pasting code and saying it fails. – meaning-matters Oct 27 '18 at 07:14
  • Yes, permission is granted and not getting any error as well and code is working fine in iOS 11, only issue comes in iOS 12 – CodeCracker Oct 27 '18 at 08:46
  • That's nasty. Do you see any iOS messages printed in the Xcode while running the code? Did you set the privacy text in your .plist file? Did you try to delete the app from your iPhone and restart your iPhone? I'd suggest to make a new app with just this code (plus code to get access to camera, ...). – meaning-matters Oct 27 '18 at 09:02
  • This happens when I add AVCaptureVideoDataOutput as output and also getting error "Finalizing CVPixelBuffer 0x618000120780 while lock count is 1". – CodeCracker Nov 03 '18 at 19:07

2 Answers2

0

Have you tried setting the illumination level with device.setTorchModeOnWithLevel(1.0)?

Apple Docs

jhenrich
  • 51
  • 4
0

When you add AVCaptureVideoDataOutputSampleBufferDelegate to get the output, you should put the torch code after session start run.

    [self.session startRunning];
    if ([self.device isTorchModeSupported:AVCaptureTorchModeOn]) {
        [self.device lockForConfiguration:nil];
        [self.device setTorchMode: AVCaptureTorchModeOn];
        [self.device setTorchModeOnWithLevel:0.01 error:nil];
        [self.device unlockForConfiguration];
    }
peter shi
  • 21
  • 1