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.

有帮助吗?

解决方案 2

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.

其他提示

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

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];
    }
}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top