gpt4 book ai didi

ios - setTorchModeOnWithLevel 不工作

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:33 24 4
gpt4 key购买 nike

我正在使用这种方法打开手电筒:

- (void)checkTorch {
for (AVCaptureDevice *device in [AVCaptureDevice devices]) {

if ([device position] != AVCaptureDevicePositionBack) {
NSLog(@"This is the front camera");
continue; // go to next device
}

NSLog(@"This is the back camera");

if([device hasTorch] == NO){
NSLog(@"this camera has no torch...");
continue; // go to next device
}

NSLog(@"The camera has a torch");

if([device isTorchAvailable] == NO){
NSLog(@"The torch is not available...");
continue; // go to next device
}

NSLog(@"The torch is available");

NSError *outError;
NSError *lockError;

[device lockForConfiguration:&lockError];
BOOL success = [device setTorchModeOnWithLevel:0.2 error:&outError];
[device unlockForConfiguration];

if (lockError)
NSLog(@"Error locking: %@", lockError.localizedDescription);

NSLog(@"Torch Level: %0.0f", device.torchLevel);

if(!success){
NSLog(@"Could not activate torch: %@", [outError localizedDescription]);
continue; // go to next device
}

NSLog(@"The torch is now active!");
}
}

这给了我这个输出(这是正确的):

2014-06-10 08:52:30.677 [291:17894] This is the back camera

2014-06-10 08:52:30.679 [291:17894] The camera has a torch

2014-06-10 08:52:30.679 [291:17894] The torch is available

2014-06-10 08:52:30.682 [291:17894] Torch Level: 0

2014-06-10 08:52:30.683 [291:17894] The torch is now active!

2014-06-10 08:52:30.683 [291:17894] This is the front camera

2014-06-10 08:52:30.684 [291:17894] This is the front camera

但是 torch 从未亮起。

-- 我正在运行测试版代码,但最新的 API 差异显示此方法或相关方法没有变化,所以我不确定为什么它不起作用。我在没有帮助的情况下在苹果开发论坛上问过。

最佳答案

我目前成功地使用以下代码控制 iPhone 5、6 和 6 Plus 上的手电筒:

- (void)setTorchToLevel:(float)torchLevel
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (torchLevel <= 0.0) {
[device setTorchMode:AVCaptureTorchModeOff];
}
else {
if (torchLevel >= 1.0)
torchLevel = AVCaptureMaxAvailableTorchLevel;
BOOL success = [device setTorchModeOnWithLevel:torchLevel error:nil];
}
[device unlockForConfiguration];
}
}

关于ios - setTorchModeOnWithLevel 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24142570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com