10

I'm using AVCaptureDevice.setTorchModeOn(level) method to turn on the flashlight at variable brightness.

On my old iPhone SE it's working fine — I can clearly see 4 different brightness levels as I change level from 0 to 1.

But on the iPhone 11 Pro the flashlight turns on only when level is 1.0! And it's brightness if far from maximum level (compared to flashlight from Control Center).

I tried using maxAvailableTorchLevel constant, but results are the same as using 1.0.
Also tried values more than 1.0 — this results in exception (as expected).

Did anyone have this problem too? Maybe there are some workarounds?

Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
  • 1
    Any update on this ? I get reports with this issue so I am on the verge o buying an iPhone 11 Pro in emergency because of this :/ – blackjack75 Feb 18 '20 at 17:51
  • Not yet unfortunately. Maybe Apple will fix it in some update… Do you know if it’s on iPhone 11 Pro only or all new phones? – Pavel Alexeev Feb 20 '20 at 04:47
  • 1
    I am not sure yet whether iPhone 11 is affected. I had complaints from users of iPhone 11 pro only. I tried some blind fixes, handling more expcetions etc but I don't know if it helped since I don't have the device yet. If you have the actual device can you check version 1.81 here and see if this helps ? I had users tell me it worked on first start but then failed on brightness change and became dim, so maybe there is a workaround. http://i.smte.ch/ledflashfree – blackjack75 Feb 21 '20 at 16:48
  • https://developer.apple.com/documentation/avfoundation/avcapturedevice/1624609-settorchmodeon Do you call lockForConfiguration() and unlockForConfiguration() before setting the torch? – antonio yaphiar Mar 06 '20 at 09:31
  • @antonioyaphiar, sure i do! – Pavel Alexeev Mar 07 '20 at 10:05
  • @blackjack75, oh, sorry I miss your comment! I just checked your Flashlight app and it's working fine on my iPhone 11 Pro — brightness is adjusting gradually from 7% (single led) till full power at 100% – Pavel Alexeev May 19 '20 at 16:46
  • Getting there! I was waiting for other reports but it seems the workaround I found helps. – blackjack75 May 20 '20 at 17:30

3 Answers3

1

I remembered that back in the iOS 3.x days we didn't have simple LED API. We had to start a full video capture session. Well it turns out that with the iPhone 11 this seems to be the only solution. I'd love to hear about others which don't require this.

This is my tested workaround. I am using Objective C here, not Swift because that's what I used in this old app from 2009! You can easily find Swift code to start video capture (and ignore the output, it should work the same.

AVCaptureSession* session = [[AVCaptureSession alloc] init];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
    [session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

CALayer *rootLayer = self.view.layer;
[rootLayer setMasksToBounds:YES];

CGRect frame = self.view.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];

//This is where you'd save the video with AVCaptureVideoDataOutput but of course we don't.

[session startRunning];

And after this you just start the LED as usual:

NSError *error = nil;

if ([inputDevice isTorchModeSupported:AVCaptureTorchModeOn])
[inputDevice setTorchModeOnWithLevel:1.0 error:&error];

This gets maximum brightness on my iPhone 11 Pro. I am now looking for the same solution without having to use the video capture (which obviously uses battery AND requires a permission, which users may not like. It needs to be explained well).

blackjack75
  • 502
  • 7
  • 12
  • 1
    This is a bug in the API on iPhone 11 Pro. I'm experiencing the same issues and used your same workaround. However, this is a regress bug and should be fixed by Apple. Please file a BUG report asap. I've done it already... the more of us reports the bug, the more likely is to be fixed. – Andrea Jun 27 '20 at 19:45
1

I just checked AVCaptureDevice.setTorchModeOn(level) on iPhone 11 Pro on iOS 14 beta 6, and it's shining bright!
There seems to be more than 4 brightness levels you can see in Control Center, and the maximum level is really bright.
Only top of two LED are working (same as flashlight in Control Center).

Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
-1

According to the documentation for maxAvailableTorchLevel

This constant always represents the maximum available torch level, independent of the actual maximum value currently supported by the device.

If this constant always represents the maximum available torch level, we not only extract that different devices have different maximum available levels but also that the device you mention can't go higher than 1.0.

Best one can do now is to reach out to Apple's developer support.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145