2013-04-23 182 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++; 
    }