Question

This is my code to change the flash mode on iphone.

In ios7.0 works: flashmode(0,1,-1)

In ios7.1 don't works: flashmode(0,0,0)

//UIImagePickerControllerCameraFlashModeOff  = -1,
//UIImagePickerControllerCameraFlashModeAuto = 0,
//UIImagePickerControllerCameraFlashModeOn   = 1

NSInteger flashMode = self.pickerReference.cameraFlashMode;

NSLog(@"flashmode %i",flashMode);

if (flashMode == UIImagePickerControllerCameraFlashModeAuto) {
    [(UIButton *)sender setImage:[UIImage imageNamed:@"flashYesComposeButton.png"] forState:UIControlStateNormal];
    self.pickerReference.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
}

else if (flashMode == UIImagePickerControllerCameraFlashModeOn) {
    [(UIButton *)sender setImage:[UIImage imageNamed:@"flashNoComposeButton.png"] forState:UIControlStateNormal];
    self.pickerReference.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
}

else if (flashMode == UIImagePickerControllerCameraFlashModeOff)
{
    [(UIButton *)sender setImage:[UIImage imageNamed:@"flashAutoComposeButton.png"] forState:UIControlStateNormal];
    self.pickerReference.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
}
Was it helpful?

Solution

I ran into that bug before

The problem is that no matter what the real flash state is in ios 7.1, it always returns UIImagePickerControllerCameraFlashModeAuto. Funny thing is, if you set the flash mode to UIImagePickerControllerCameraFlashModeOff, the flash will really be off, but if you ask the state, it will still be UIImagePickerControllerCameraFlashModeAuto.

So I solved my problem by keeping my own "flashState". When, clicked, I manually change my own state and never trust the one sent back but the UIImagePickerController.

@property (nonatomic) UIImagePickerControllerCameraFlashMode flashMode;

when the button is clicked :

       if (self.flashMode == UIImagePickerControllerCameraFlashModeAuto) {  
            //toggle your button to "on"
            self.flashMode = UIImagePickerControllerCameraFlashModeOn;
        }else if (self.flashMode == UIImagePickerControllerCameraFlashModeOn){  
            //toggle your button to "Off"
            self.flashMode = UIImagePickerControllerCameraFlashModeOff;
        }else if (self.flashMode == UIImagePickerControllerCameraFlashModeOff){ 
            //toggle your button to "Auto"
            self.flashMode = UIImagePickerControllerCameraFlashModeAuto;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top