2014-11-25 146 views
0

我正在使用Apple的AVCam source code来创建自定义相机,我试图切换闪光灯开/关,但不工作。这是我的代码,不知道有什么问题。我是AVCam的新手。使用AVCam切换开/关闪光灯

- (void) toggleFlash:(id)sender { 
    dispatch_async([self sessionQueue], ^{ 
     AVCaptureDevice *currentVideoDevice = [[self videoDeviceInput] device]; 
     AVCaptureDevicePosition currentPosition = [currentVideoDevice position]; 
     if(currentPosition == AVCaptureDevicePositionUnspecified || currentPosition == AVCaptureDevicePositionBack) { 
      if([currentVideoDevice hasFlash]) { 
       [currentVideoDevice lockForConfiguration:nil]; 
       [currentVideoDevice setFlashMode:AVCaptureFlashModeOn]; 
       [currentVideoDevice unlockForConfiguration]; 
      } 
     } 
    }); 
} 

它通过代码中的每一行,并没有记录任何错误,但仍然没有运气。

回答

0
- (void) toggleFlash { 
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    if ([device hasTorch] && [device hasFlash]){ 
     [device lockForConfiguration:nil]; 
     [device setTorchMode:!device.torchActive]; 
     [device setFlashMode:!device.torchActive]; 
     [device unlockForConfiguration]; 
    } 
} 

P.S.就我而言,火炬/闪光灯最初是关闭的。