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

前端之家收集整理的这篇文章主要介绍了为Xcode中的iPhone 5手电筒应用调暗LED前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我期待用滑块选项调暗手电筒的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];
}
原文链接:https://www.f2er.com/iOS/332283.html

猜你在找的iOS相关文章