Search code examples
iosmarmaladeios-cameracamera-flash

Camera Flash is not working


I am trying to make an extension for marmalade, which turns on and off the camera flash in iPhone. I am referring to this answer on SO about using camera flash. I've put the exact code what he has mentioned and [device setTorchMode:AVCaptureTorchModeOn]; gets called too. But the flash doesn't respond, as if nothing has happened. Is there anything I need to do, to make it work as a static library, so that I can use it in my extension?

Update:-
I am using iOS-SDK 6.1 to compile the extension and was testing on iPhone 4 (iOS version 4.3.1). But now I am testing on iPhone 5 (iOS version 6.0.0), and now the flash is turning on, but not turning off. I guess this might help.


Solution

  • I don't know why, but the below code worked for me.

    void TurnFlashOn_platform(bool turnOn) { 
        AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]){
            [device lockForConfiguration:nil]; //you must lock before setting torch mode
            [device setTorchMode:turnOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }
    }
    

    I guess must be some typos. Thanks anyways.