user3344977
user3344977

Reputation: 3604

Flash does not work when using front facing camera with AVFoundation

I have a camera app that is using AVFoundation. When the user is taking a photo, they can press a button to toggle the flash on and off. Right now this works perfectly for the rear facing camera, but I cannot get it to work for the front facing camera.

No matter what I do, the front facing camera will not use a flash.

Here is the code I am using to toggle flash on and off on the button press:

-(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];
}
  }

}

Upvotes: 1

Views: 3259

Answers (3)

lootsch
lootsch

Reputation: 1865

Are you sure, that you debugged correctly? If I'm running this shortened version of your code on my 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;
    }

}

I get this result:

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!

So there's no front camera flash, and no bug by apple.

Upvotes: 0

dejix
dejix

Reputation: 1144

Although its an old thread, for anyone that currently want to "imitate" Flash on Front Camera with the switching white screen and is confused with the device's hasFlash property :

The iPhone 6s and 6s Plus front-facing cameras are the first front-facing iOS cameras to respond YES to the -hasFlash property.

Source : https://forums.developer.apple.com/thread/21694 (Paragraph : Retina Flash)

Upvotes: 1

Rafał Sroka
Rafał Sroka

Reputation: 40028

There is no iOS device with a flash on the front side. If it's like you said, that the hasFlash method returns YES for front-facing camera then this is a bug. Consider filing a radar to Apple. You have the sample code that illustrates the issue so you can just attach it.

Upvotes: 0

Related Questions