我正在做一个需要使用层次结构验证二维码的iOS应用程序:
View
---Scan View
---Image View - cardBG
---Inside View
问题在于第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 :(得分: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 :(得分:1)
以下代码可以帮助您:
if(self.previewLayer) {
[self.previewLayer removeFromSuperlayer];
self.previewLayer = nil;
}
if(_captureSession) {
[_captureSession stopRunning];
_captureSession = nil;
}