我希望用滑块选项调暗手电筒的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];
}
}
}
答案 0 :(得分:13)
- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(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];
}
答案 1 :(得分:0)
如果有两个滑块,那是因为滑块设置在m中。文件,您必须删除代码的[self.view addSubview:slider];
部分。