在Xcode中为iPhone 5手电筒应用调暗LED

use*_*444 7 iphone xcode ios ios6

我期待用滑块选项调暗手电筒的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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*tay 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];
Run Code Online (Sandbox Code Playgroud)

编辑 - 完整示例:

这是一个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];
Run Code Online (Sandbox Code Playgroud)

然后,响应滑块更改:

- (void)sliderDidChange:(UISlider *)slider
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    [device setTorchModeOnWithLevel:slider.value error:NULL];
    [device unlockForConfiguration];
}
Run Code Online (Sandbox Code Playgroud)