2013-04-23 142 views
0

如何执行闪光灯,如本机相机应用执行一个动作? 我发现了以下代码来打开灯光,但我需要拍摄闪光灯。像原生相机应用那样执行闪光灯(灯光)

if(self.videoDevice.hasTorch) { 
    [self.videoDevice lockForConfiguration:nil]; 
    [self.videoDevice setTorchModeOnWithLevel: 1.0 error: nil]; 
    [self.videoDevice unlockForConfiguration]; 
} 

回答

0

你将不得不再次0.0之间设置torchLevel到1.0再到0.0一个非常小的区间。

- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError 
+0

这也是我的想法,但我认为整个机制比似乎更复杂,因为所有事情都需要以特定方式进行计时。 – 2013-04-23 09:51:45

0

试试这个代码:

- (void)turnOnCamerFlash 
{ 
    if (NSClassFromString(@"AVCaptureDevice") != nil) 
    { 
     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

     if ([device hasTorch]) 
     { 
      [device lockForConfiguration:nil]; 
      [device setTorchMode:AVCaptureTorchModeOn]; 
      [device unlockForConfiguration]; 
     }  
    } 
} 
+0

这只是打开闪光灯,但我需要模拟像本机闪光灯:) – 2013-04-23 10:42:14

0

它接缝,有在那里不闪功能,所以(只?)可能进行闪光灯使用计时器。

- (IBAction)doFlash:(id)sender { 
     if(self.videoDevice.hasTorch) { 
      flashCounter = 0; 
      [NSTimer scheduledTimerWithTimeInterval: 0.1 
              target:self 
              selector:@selector(flashLightTicker:) 
              userInfo:nil 
              repeats: YES]; 
     } 
    } 

    - (void)flashLightTicker:(id)sender { 
     [self.videoDevice lockForConfiguration:nil]; 
     if(flashCounter == 0) { 
      [self.videoDevice setTorchModeOnWithLevel: 0.1 error: nil]; 
     } 
     if(flashCounter == 5) { 
      [self.videoDevice setTorchMode: AVCaptureTorchModeOff]; 
     } 
     if(flashCounter == 7) { 
      [self.videoDevice setTorchModeOnWithLevel: AVCaptureMaxAvailableTorchLevel error: nil]; 
     } 
     if(flashCounter >= 10) { 
      [self.videoDevice setTorchMode: AVCaptureTorchModeOff]; 
      [sender invalidate]; 
     } 
     [self.videoDevice unlockForConfiguration]; 
     flashCounter++; 
    }