iOS:iPhone 11 Pro 上的手电筒级别

Pav*_*eev 10 iphone ios avcapture avcapturedevice flashlight

我正在使用AVCaptureDevice.setTorchModeOn(level)方法以可变亮度打开手电筒。

在我的旧 iPhone SE 上,它运行良好——当我level0变为 时,我可以清楚地看到 4 种不同的亮度级别1

但在 iPhone 11 Pro 上,手电筒仅在水平为1.0! 如果远离最高水平,它的亮度(与控制中心的手电筒相比)。

我尝试使用maxAvailableTorchLevel常量,但结果与使用1.0.
还尝试了超过的值1.0- 这会导致异常(如预期的那样)。

有人也有这个问题吗?也许有一些解决方法?

bla*_*k75 1

我记得在 iOS 3.x 时代我们还没有简单的 LED API。我们必须开始完整的视频捕捉会话。事实证明,对于 iPhone 11,这似乎是唯一的解决方案。我很想听听其他不需要这个的人的情况。

这是我经过测试的解决方法。我在这里使用 Objective C,而不是 Swift,因为这是我在 2009 年的旧应用程序中使用的!您可以轻松找到 Swift 代码来启动视频捕获(并忽略输出,它应该工作相同。

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];
Run Code Online (Sandbox Code Playgroud)

之后,您只需像往常一样启动 LED:

NSError *error = nil;

if ([inputDevice isTorchModeSupported:AVCaptureTorchModeOn])
[inputDevice setTorchModeOnWithLevel:1.0 error:&error];
Run Code Online (Sandbox Code Playgroud)

这在我的 iPhone 11 Pro 上获得了最大亮度。我现在正在寻找相同的解决方案,而不必使用视频捕获(这显然会使用电池并且需要许可,用户可能不喜欢这一点。它需要得到很好的解释)。