Search code examples
iosuiimagepickercontrollerios10camera-flash

iOS 10: UIImagePickerViewController Can Not Set Flash Mode


I am working on creating custom controls for my UIImagePickerViewController. I have the default flash setting set to .off and I want to switch between on and off.

But I'm having a lot of trouble getting the UIImagePickerControllerCameraFlashMode to toggle from on to off. This should seemingly be a very easy thing to set. But I have been trying and I can not get the following to change the flash mode, it just stays in the .off mode.

func didTapFlashButton() -> Bool {
    if let picker = mediaPicker {
        if picker.cameraFlashMode == .on || picker.cameraFlashMode == .auto {
            picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.off
            return false
        }
        else {
            picker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.on
            return true
        }
    }
    return false
}

Why is this not updating the flash mode on my UIImagePickerController?


EDIT (answer to comment)

Yes I have checked if the code executes and it always executes the else block to set it to .on. So the UIImagePickerController correctly acknowledges that the default flash mode I set is .false (verified via print statement).

But, whenever I tap the button to change flash mode to .on the flash never works. This meaning it never updated the flash mode on the UIImagePickerController.

I don't know why this isn't working and I can't find any other way to set the flash mode other than what I'm doing.


Solution

  • This one actually made me feel kind of stupid. Shortly after posting this question I realized that the device I was testing on did not have flash capabilities. What you should do if you are using flash mode is to first ask the UIImagePickerController class if the device is able to use flash in the first place. Documentation.