我正在使用 AVCaptureDevice.setTorchModeOn(level)
方法以可变亮度打开手电筒。
在我的旧 iPhone SE 上,它工作正常 - 当我将 level
从 0
更改为 1
时,我可以清楚地看到 4 个不同的亮度级别。
但在 iPhone 11 Pro 上,只有当级别为 1.0
时,手电筒才会打开!如果它的亮度远离最大级别(与控制中心的手电筒相比)。
我尝试使用 maxAvailableTorchLevel
常量,但结果与使用 1.0
相同。
还尝试了大于 1.0
的值 - 这会导致异常(如预期)。
有人也遇到这个问题吗?也许有一些解决方法?
最佳答案
我记得在 iOS 3.x 时代我们还没有简单的 LED API。我们必须开始完整的视频捕捉 session 。事实证明,对于 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];
此后,您只需像往常一样启动 LED:
NSError *error = nil;
if ([inputDevice isTorchModeSupported:AVCaptureTorchModeOn])
[inputDevice setTorchModeOnWithLevel:1.0 error:&error];
这在我的 iPhone 11 Pro 上获得了最大亮度。我现在正在寻找相同的解决方案,而无需使用视频捕获(这显然会使用电池并且需要权限,用户可能不喜欢这一点。需要对其进行很好的解释)。
关于iOS:iPhone 11 Pro 上的 torch 级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60197045/