AVCaptureSession – 停止运行 – 需要很长时间

我使用ZXing的应用程序,这是主要是相同的代码比ZXing原来的代码,除了我允许连续扫描几次(即,ZXingWidgetController是不是必然被排除一旦检测到)。

当我按下那个呼叫的解除button时,我经历了很长时间的冻结(有时它永远不会结束)

- (void)cancelled { // if (!self.isStatusBarHidden) { // [[UIApplication sharedApplication] setStatusBarHidden:NO]; // } [self stopCapture]; wasCancelled = YES; if (delegate != nil) { [delegate zxingControllerDidCancel:self]; } } 

 - (void)stopCapture { decoding = NO; #if HAS_AVFF 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.prevLayer removeFromSuperlayer]; /* // heebee jeebees here ... is iOS still writing into the layer? if (self.prevLayer) { layer.session = nil; AVCaptureVideoPreviewLayer* layer = prevLayer; [self.prevLayer retain]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 12000000000), dispatch_get_main_queue(), ^{ [layer release]; }); } */ self.prevLayer = nil; self.captureSession = nil; #endif } 

(请注意,删除视图的dismissModalViewController是在委托方法内)

我只有在连续数次扫描的情况下才会遇到冻结问题,而且只有使用iPhone 4(不冻结4S)

任何想法 ?

干杯

只读存储器

根据AV Cam View控制器示例调用startRunning或stopRunning不会返回,直到会话完成请求的操作。 由于您正在将这些消息发送到主线程上的会话,因此它会冻结所有UI,直到请求的操作完成。 我会build议您将您的调用包装在asynchronous调度中,以便视图不会locking。

 - (void)cancelled { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self stopCapture]; }); //You might want to think about putting the following in another method //and calling it when the stop capture method finishes wasCancelled = YES; if (delegate != nil) { [delegate zxingControllerDidCancel:self]; } }