我期待用滑块选项调暗手电筒的LED.我知道Apple支持iOS 6但是,我不确定要使用哪些代码.这是我目前在.m文件中的代码.
-(IBAction)torchOn:(id)sender; { onButton.hidden = YES; offButton.hidden = NO; onView.hidden = NO; offView.hidden = YES; AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn]) { BOOL success = [flashLight lockForConfiguration:nil]; if(success) { [flashLight setTorchMode:AVCaptureTorchModeOn]; [flashLight unlockForConfiguration]; } } } -(IBAction)torchOff:(id)sender; { onButton.hidden = NO; offButton.hidden = YES; onView.hidden = YES; offView.hidden = NO; AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn]) { BOOL success = [flashLight lockForConfiguration:nil]; if(success) { [flashLight setTorchMode:AVCaptureTorchModeOff]; [flashLight unlockForConfiguration]; } } }
解决方法
– (BOOL)setTorchModeOnWithLevel:(float)torchLevel错误:(NSError **)outError
你想要的是什么但是,从我所看到的,它只在某些间隔(~0.2)更新.
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchModeOnWithLevel:slider.value error:NULL]; [device unlockForConfiguration];
编辑 – 完整示例:
这是一个UiSlider.您需要在滑块上添加IBAction插座或以编程方式添加目标(就像我一样):
UiSlider *slider = [[UiSlider alloc] initWithFrame:CGRectMake(20.0f,20.0f,280.0f,40.0f)]; slider.maximumValue = 1.0f; slider.minimumValue = 0.0f; [slider setContinuous:YES]; [slider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider];
然后,响应滑块更改:
- (void)sliderDidChange:(UiSlider *)slider { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; [device lockForConfiguration:nil]; [device setTorchModeOnWithLevel:slider.value error:NULL]; [device unlockForConfiguration]; }