views:

175

answers:

1
+3  A: 
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
Do i need to alloc init AVCaptureDevice?
Cocoa Dev
no, you should be able to get one from `[AVCaptureDevice devices]`
slf
@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
lockConfiguration is just a simple call, but don't forget to unlock
slf
perhaps you want Torch? That should keep the LED on the entire time
slf
try `NSLog(@"%@", currentDevice)` while looping through `allDevices` for more info
slf
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
added locking example
slf