使用自定义相机覆盖更改闪光模式

时间:2014-01-25 21:10:11

标签: ios flash button camera overlay

我正在使用带有自定义相机覆盖视图的UIImagePickerController,并希望添加自定义按钮以在自动,开启和关闭之间切换闪光模式。这是按钮调用的方法:

- (void)didTapFlash:(id)sender
{
    if (self.imagePickerController.cameraFlashMode == UIImagePickerControllerCameraFlashModeAuto)
    {
        [imagePickerController setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconOn.png"] forState:UIControlStateNormal];
    }
    else if (self.imagePickerController.cameraFlashMode == UIImagePickerControllerCameraFlashModeOn)
    {
        self.imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconOff.png"] forState:UIControlStateNormal];
    }
    else if (self.imagePickerController.cameraFlashMode == UIImagePickerControllerCameraFlashModeOff)
    {
        self.imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconAuto.png"] forState:UIControlStateNormal];
    }
}

问题是这不起作用。一旦呈现控制器,self.imagePickerController.cameraFlashMode的值就不会改变。闪存值可以在显示之前更改,但一旦显示,它将始终返回值0(自动)。奇怪的是,即使值没有改变,如果我将上面的代码改为:

,闪存的行为也就像它一样。
- (void)didTapFlash:(id)sender
{
    if (flashButton.tag == 0)
    {
        [imagePickerController setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
        self.flashButton.tag = 1;
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconOn.png"] forState:UIControlStateNormal];
    }
    else if (flashButton.tag == 1)
    {
        self.imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
        self.flashButton.tag = -1;
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconOff.png"] forState:UIControlStateNormal];
    }
    else if (flashButton.tag == -1)
    {
        self.imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        self.flashButton.tag = 0;
        [self.flashButton setImage:[UIImage imageNamed:@"flashIconAuto.png"] forState:UIControlStateNormal];
    }
} 

一切都按预期工作。 (flashButton.tag只是存储flash值)

有谁知道这里发生了什么?为什么cameraFlashMode的值总是为0(在NSLog语句中和直接通过调试器检查时),即使闪存表现得好像设置正确?

1 个答案:

答案 0 :(得分:1)

这似乎是最新iOS版本中的一个错误,

您会注意到它可以启用

标志
 self.picker.showsCameraControls = true

但它显示了你的overlayview的原生相机控件,所以我用这段代码攻击它:

 self.picker.showsCameraControls = true
 self.picker.cameraFlashMode = .On
 self.picker.showsCameraControls = false

它足够快,不会因显示和隐藏控件而干扰用户。 它为我工作,我相信它也适用于您的情况!