11

I've set an AVCaptureDevice TorchMode to AVCaptureTorchModeAuto, the torch mode is set after the AVCaptureSession has started running. I'd expected the torch mode to light-up the LED in low light conditions as per Apple's documentation: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/Reference/Reference.html#//apple_ref/doc/c_ref/AVCaptureTorchMode

However, the torch does not turn on in any light conditions on my test devices: iPhone 4S, iPhone 5. Has anyone had this issue?

Here's my code:

- (void)enableTorchMode
{
    if ((self.device.hasTorch) && ([self.device isTorchModeSupported:AVCaptureTorchModeAuto]))
    {
        [self.device lockForConfiguration:nil];
        self.device.torchMode = AVCaptureTorchModeAuto;
        [self.device unlockForConfiguration];
    }
}
user3183222
  • 143
  • 6

1 Answers1

9

You don't mention what you're doing with the camera, but currently (as of iOS 7.1), the auto torch mode doesn't work unless the AVCaptureSession has a video output. This made sense when the only other options were photos or audio, but if you're only interested in metadata like faces or barcodes, then it's a problem.

You can use something like this when you're setting up the session to force it to work:

if ([captureDevice isTorchModeSupported:AVCaptureTorchModeAuto]) {
    AVCaptureOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:videoOutput];
}

If this is the problem you're having, I'd recommend filing a bug report. Feel free to dupe mine.

robotspacer
  • 2,732
  • 2
  • 28
  • 50
  • Unlike what is stated in the documentation, even with a video output, the light sampling is only done at the start of the session, not continuously (as documented). See [here](http://stackoverflow.com/questions/22998471/avcapturetorchmodeauto-does-not-continously-update-torch-mode/28265053#28265053). – Randomblue Feb 01 '15 at 16:52
  • Thank you for the answer, the torch is working now (yes although it captures to turn on/off torch only first time it initialize, but at least it works) – green0range Mar 06 '20 at 20:40