2014-02-18 64 views
1

我這樣做,需要對QR代碼驗證與層次結構類似這樣的iOS應用程序:如何停止AVCaptureSession正確

View 
---Scan View 
---Image View - cardBG 
---Inside View 
  1. 當視圖加載,掃描查看被隱藏。
  2. 當用戶點擊按鈕掃描時,內部視圖和圖像視圖被設置爲隱藏,顯示掃描視圖。
  3. 掃描完成後,內部和圖像再次出現。

的問題是,在步驟3:當我停止AVCaptureSession,即使在像in this question異步調度,它需要8-10秒刷新視圖。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    if([_captureSession isRunning])[_captureSession stopRunning]; 
    AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0]; 
    [_captureSession removeInput:input]; 
    AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0]; 
    [_captureSession removeOutput:output]; 
}); 

[self.bgImageView setHidden:NO]; 
[self.insideView setHidden:NO]; 
[self.scanView setHidden:YES]; 
[self.previewLayer removeFromSuperlayer]; 

我的問題是:如何獲得視圖以避免這種凍結?

+0

可能是你通過這個線程 http://stackoverflow.com/questions/3741121/how-to-properly-release找到答案-an-avcapturesession 謝謝。 –

回答

2

很難說沒有更多的上下文。取決於實際造成延遲的原因。會有這樣的工作嗎?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    if([_captureSession isRunning])[_captureSession stopRunning]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.bgImageView setHidden:NO]; 
     [self.insideView setHidden:NO]; 
     [self.scanView setHidden:YES]; 
     [self.previewLayer removeFromSuperlayer];   
    }); 

    AVCaptureInput* input = [_captureSession.inputs objectAtIndex:0]; 
    [_captureSession removeInput:input]; 
    AVCaptureVideoDataOutput* output = (AVCaptureVideoDataOutput*)[_captureSession.outputs objectAtIndex:0]; 
    [_captureSession removeOutput:output];  

}); 
1

下面的代碼將幫助您:

if(self.previewLayer) { 
    [self.previewLayer removeFromSuperlayer]; 
    self.previewLayer = nil; 
} 

if(_captureSession) { 
    [_captureSession stopRunning]; 
    _captureSession = nil; 
} 
+0

謝謝。這也幫助我。原因是我試圖更新UI時,不在主線程。 – anhdat