0
-(IBAction)turningFlashOn:(id)sender
{
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];

if (videoInput) {
    [captureSession addInput:videoInput];



    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    [videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];
    [captureSession addOutput:videoOutput];
    [captureSession startRunning];
    videoCaptureDevice.torchMode = AVCaptureFlashModeOn;
}
}

I am being asked to use lockForConfiguration but it doesn't work or maybe i'm using it wrong. Can anyone please tell me what I'm doing wrong?

Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177

2 Answers2

2
if([videoCaptureDevice lockForConfiguration]) {
  [videoCaptureDevice setTorchMode:AVCaptureTorchModeOn];
  [videoCaptureDevice unlockForConfiguration];
 }
gnuchu
  • 1,496
  • 13
  • 21
  • [videoCaptureDevice setTorchMode:AVCaptureTorchModeOff]; or just release your AVCaptureSession object. Which ever is more appropriate. – gnuchu Jul 14 '10 at 09:12
  • You should probably call `[session stopRunning];` before releasing the session. See my post here: http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424#3367424 – iwasrobbed Jul 30 '10 at 00:26
1
- (void)flashLightOn {


    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device hasFlash] == YES) {

            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOn];
            [device unlockForConfiguration];
        }

    }
}

-(void)flashLightOff {


    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device hasFlash] == YES) {

            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOff];
            [device unlockForConfiguration];
        }

    }

}
Nikkie
  • 93
  • 8