谁能告诉我如何使用以下方法的示例:
(BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
谁能告诉我如何使用以下方法的示例:
(BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
在此处查看 Apple 的参考资料:AVCaptureDevice
首先导入 AVFoundation 并将其添加到构建阶段。
#import <AVFoundation/AVFoundation.h>
然后使用以下代码:
//Get all devices (front and back camera)
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");
//get device dependent maximum (between 0 and 1)
float level = AVCaptureMaxAvailableTorchLevel;
NSError* outError;
BOOL success = [device setTorchModeOnWithLevel:level error:&outError];
if(!success){
NSLog(@"Could not activate torch: %@", [outError localizedDescription]);
continue; // go to next device
}
NSLog(@"The torch is now active!");
}