我怎样才能像原生相机应用程序一样执行闪光灯?我找到了以下代码来打开灯,但我需要拍一张闪光灯。
if(self.videoDevice.hasTorch) {
[self.videoDevice lockForConfiguration:nil];
[self.videoDevice setTorchModeOnWithLevel: 1.0 error: nil];
[self.videoDevice unlockForConfiguration];
}
我怎样才能像原生相机应用程序一样执行闪光灯?我找到了以下代码来打开灯,但我需要拍一张闪光灯。
if(self.videoDevice.hasTorch) {
[self.videoDevice lockForConfiguration:nil];
[self.videoDevice setTorchModeOnWithLevel: 1.0 error: nil];
[self.videoDevice unlockForConfiguration];
}
您必须将 torchLevel 设置在 0.0 到 1.0 之间,然后再设置为 0.0,时间间隔非常小。
- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
试试这个代码:
- (void)turnOnCamerFlash
{
if (NSClassFromString(@"AVCaptureDevice") != nil)
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
}
}
那里似乎没有闪光功能,所以(唯一?)执行闪光的可能性是使用计时器。
- (IBAction)doFlash:(id)sender {
if(self.videoDevice.hasTorch) {
flashCounter = 0;
[NSTimer scheduledTimerWithTimeInterval: 0.1
target:self
selector:@selector(flashLightTicker:)
userInfo:nil
repeats: YES];
}
}
- (void)flashLightTicker:(id)sender {
[self.videoDevice lockForConfiguration:nil];
if(flashCounter == 0) {
[self.videoDevice setTorchModeOnWithLevel: 0.1 error: nil];
}
if(flashCounter == 5) {
[self.videoDevice setTorchMode: AVCaptureTorchModeOff];
}
if(flashCounter == 7) {
[self.videoDevice setTorchModeOnWithLevel: AVCaptureMaxAvailableTorchLevel error: nil];
}
if(flashCounter >= 10) {
[self.videoDevice setTorchMode: AVCaptureTorchModeOff];
[sender invalidate];
}
[self.videoDevice unlockForConfiguration];
flashCounter++;
}