我正在尝试为我的 iPhone 制作一个手电筒应用程序。我有一部 iPhone 4,想在我的项目中使用 iPhone 上的 LED。任何人都可以帮助我开始使用它吗?
最佳答案
这是一个较短的版本,您现在可以使用它来打开或关闭 LED:
- (void)torchOnOff: (BOOL) onOff
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
更新:(2015 年 3 月)
您还可以设置手电筒的亮度:
- (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];
}
}
关于iphone - 激活手电筒应用程序的 LED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3253187/