2014-11-25 145 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.就我而言,火炬/閃光燈最初是關閉的。