使用带AVFoundation的前置摄像头时,闪光灯不起作用

时间:2014-03-08 07:30:29

标签: ios objective-c avfoundation avcapturedevice ios-camera

我有一个使用AVFoundation的相机应用程序。当用户拍照时,他们可以按一个按钮来打开和关闭闪光灯。现在这适用于后置摄像头,但我不能让它适用于前置摄像头。

无论我做什么,前置摄像头都不会使用闪光灯。

以下是我用来在按下按钮时打开和关闭闪光灯的代码:

-(IBAction)toggleFlash {

NSLog(@"Toggle flash button has been pressed");

NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
    if ([device hasFlash] == YES) {

        NSLog(@"Current Device Flash Mode: %d", device.flashMode);


        [device lockForConfiguration:nil];


        if(device.flashMode == 0) {

            [device setFlashMode:AVCaptureFlashModeOn];

            NSLog(@"New device flash mode: %d", device.flashMode);



        } else if (device.flashMode == 1) {

            [device setFlashMode:AVCaptureFlashModeOff];

            NSLog(@"New device flash mode: %d", device.flashMode);


        } else if (device.flashMode == 2) {

            [device setFlashMode:AVCaptureFlashModeOn];


        }

        [device unlockForConfiguration];
}
  }

}

3 个答案:

答案 0 :(得分:1)

虽然它是一个老线程,但对于那些目前想要模仿"前置摄像头上的闪光灯,带有切换白屏,并与设备的闪光属性混淆:

  

iPhone 6s和6s Plus前置摄像头是第一款   前置iOS摄像头对-hasFlash属性作出YES响应。

来源:https://forums.developer.apple.com/thread/21694(段落:Retina Flash)

答案 1 :(得分:0)

正面没有带闪光灯的iOS设备。如果像你说的那样,hasFlash方法为前置摄像头返回YES那么这就是一个错误。考虑向Apple提交radar。您有示例代码来说明问题,因此您只需附加它即可。

答案 2 :(得分:0)

您确定,您调试正确吗?如果我在iPhone上运行这个缩短版的代码:

- (IBAction)button:(id)sender
{
    NSLog(@"Toggle flash button has been pressed");

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    int i = 0;

    for (AVCaptureDevice *device in devices)
    {
        if ([device hasFlash] == YES)
            NSLog(@"Device %d has flash!", i);

        else
            NSLog(@"Device %d has no flash!", i);
        ++i;
    }

}

我得到了这个结果:

2014-03-08 09:45:48.551 test[12162:60b] Toggle flash button has been pressed
2014-03-08 09:45:48.597 test[12162:60b] Device 0 has flash!
2014-03-08 09:45:48.600 test[12162:60b] Device 1 has no flash!

所以没有前置摄像头闪光灯,也没有苹果的错误。