2010-07-08 22 views
3

我正在制作一个应用程序,允许我将图像拼接到一个全景场景中。我希望能够以编程方式打开iPhone 4上的Flash LED。如何使用AVCaptureFlashMode

我该怎么做?

我阅读文档,发现我需要使用AVCaptureFlashMode

,但我无法弄清楚如何使用2呢?

任何帮助,将不胜感激。


更新了下面的代码。谢谢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); 
    } 

如何关闭它?

+0

请在这里看到我的回答对未来如何开/关切换参考:http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424# 3367424 – iwasrobbed 2010-07-29 22:20:22

回答

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

我需要分配init AVCaptureDevice吗? – 2010-07-08 14:16:58

+0

不,你应该可以从'[AVCaptureDevice devices]'获得一个' – slf 2010-07-08 16:54:09

+0

@SIF你能否更新你的示例代码来演示如何开启闪存?请不要以为我获得了AVCaptureDevice。 lockConfiguration方法怎么样? – 2010-07-08 19:06:20