2

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.

Community
  • 1
  • 1
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184

3 Answers3

3

That code you copied may have an error in it. Try the following:

-(void)turnOnFlash
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        if ([device hasTorch]) {
            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }
}
dana0550
  • 1,125
  • 1
  • 9
  • 16
3

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.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
2

Use this function to turn on and off the flash light...

#import <AVFoundation/AVFoundation.h>


- (void) turnTorchOn: (bool) on {

Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch] && [device hasFlash]){

        [device lockForConfiguration:nil];
        if (on) {
            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];
            torchIsOn = YES;
        } else {
            [device setTorchMode:AVCaptureTorchModeOff];
            [device setFlashMode:AVCaptureFlashModeOff];
            torchIsOn = NO;            
        }
        [device unlockForConfiguration];
    }
}
}
Divyu
  • 1,325
  • 9
  • 21
  • Now that's exactly the same answer I was referring too. Can You point out what problem it may have, while using this method statically? – 0xC0DED00D May 15 '13 at 06:14
  • I don't understand what is the actual problem you are facing while using this code.. can u tell me? – Divyu May 15 '13 at 07:17
  • It's not working. No flash blink, despite all methods running perfectly. – 0xC0DED00D May 15 '13 at 07:27