4

I'm making an app to allow me to stitch images together into a panoramic scene. I want to be able to turn the Flash LED on the iPhone 4 programatically.

How can I do this?

I read the documentation and discovered that I need to use AVCaptureFlashMode

but I can't figure out how 2 use it?

any help would be appreciated.


Updated Code below. Thanks SIF!

NSError* error = nil;
    NSLog(@"Setting up LED");

    if([captDevice hasTorch] == NO)
    {
        NSLog(@"Error: This device doesnt have a torch");
    }
    if([captDevice isTorchModeSupported:AVCaptureTorchModeOn] == NO)
    {
        NSLog(@"Error: This device doesnt support AVCaptureTorchModeOn");
    }

    AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
    AVCaptureDeviceInput* videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captDevice error:&error];
    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];

    if (videoInput && videoOutput) 
    {
        [captureSession addInput:videoInput];
        [captureSession addOutput:videoOutput];
        if([captDevice lockForConfiguration:&error])
        {
            if (flag == YES) {
                captDevice.torchMode = AVCaptureTorchModeOn;
            } else {
                captDevice.torchMode = AVCaptureTorchModeOff;
            }           
            [captDevice unlockForConfiguration];
        }
        else 
        {
            NSLog(@"Could not lock device for config error: %@", error);
        }
        [captureSession startRunning];
    }
    else 
    {
        NSLog(@"Error: %@", error);
    }

How do you turn it off?

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
  • Please see my answer here for future reference on how to toggle on/off: http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424#3367424 – iwasrobbed Jul 29 '10 at 22:20

1 Answers1

7
AVCaptureDevice* d = nil;

// find a device by position
NSArray* allDevices = [AVCaptureDevice devices];
for (AVCaptureDevice* currentDevice in allDevices) {
  if (currentDevice.position == AVCaptureDevicePositionBack) {
    d = currentDevice;
  }
}

// at this point, d may still be nil, assuming we found something we like....

NSError* err = nil;
BOOL lockAcquired = [d lockForConfiguration:&err];

if (!lockAcquired) {
   // log err and handle...
} else {
   // flip on the flash mode
   if ([d hasFlash] && [d isFlashModeSupported:AVCaptureFlashModeOn] ) {
      [d setFlashMode:AVCaptureFlashModeOn];
   }

   [d unlockForConfiguration];
}
slf
  • 22,595
  • 11
  • 77
  • 101
  • Do i need to alloc init AVCaptureDevice? – Cocoa Dev Jul 08 '10 at 14:16
  • no, you should be able to get one from `[AVCaptureDevice devices]` – slf Jul 08 '10 at 16:54
  • @SIF Can you please update your sample code to demonstrate how the flash is turned on? Please don't assume that I obtained the AVCaptureDevice. How about the lockConfiguration method? – Cocoa Dev Jul 08 '10 at 19:06
  • lockConfiguration is just a simple call, but don't forget to unlock – slf Jul 08 '10 at 21:48
  • 1
    perhaps you want Torch? That should keep the LED on the entire time – slf Jul 08 '10 at 21:48
  • try `NSLog(@"%@", currentDevice)` while looping through `allDevices` for more info – slf Jul 08 '10 at 21:51
  • Thanks but I keep getting *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You must call lockForConfiguration and successfully obtain the configuration lock before setting flashMode:' – Cocoa Dev Jul 09 '10 at 14:14