2014-02-18 65 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