2

我们可以控制iphone相机的闪光灯强度吗?我正在为眼睛视网膜开发 Iphone 应用程序,因为我需要控制光强度(增加或减少闪光灯)但我找不到任何相关的东西我不知道苹果是否允许这样做请帮助我找到这个解决方案. 谢谢你。

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
  [device lockForConfiguration:nil];
  [device setTorchMode:AVCaptureTorchModeOff];
  [device unlockForConfiguration];
}

我正在使用此代码打开闪光灯。我设法通过添加这些代码行来解决这个问题。

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 280.0f, 40.0f)];
    slider.maximumValue = 1.0f;
    slider.minimumValue = 0.2f;
    [slider setContinuous:YES];
    [slider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];

    AVCaptureDevice *flashLight = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if([flashLight isTorchAvailable] && [flashLight isTorchModeSupported:AVCaptureTorchModeOn])
    {
        BOOL success = [flashLight lockForConfiguration:nil];
        if(success)
        {
            [flashLight setTorchMode:AVCaptureTorchModeOn];
            [flashLight unlockForConfiguration];
        }
    }
And then write this method somewhere in your code
- (void)sliderDidChange:(UISlider *)slider
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];
    [device setTorchModeOnWithLevel:slider.value error:NULL];
    [device unlockForConfiguration];
}
4

0 回答 0