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

Rom*_*nge 3 iphone zxing ios avcapturesession

我使用ZXing作为应用程序,这主要是与ZXing原始代码相同的代码,除了我允许连续扫描几次(即,一旦检测到某些东西,ZXingWidgetController就不会被解雇).

当我按下调用的关闭按钮时,我会经历长时间的长时间冻结(有时它永远不会结束)

- (void)cancelled {
  //  if (!self.isStatusBarHidden) {
  //      [[UIApplication sharedApplication] setStatusBarHidden:NO];
  //  }

    [self stopCapture];

    wasCancelled = YES;
    if (delegate != nil) {
        [delegate zxingControllerDidCancel:self];
    }


} 
Run Code Online (Sandbox Code Playgroud)

- (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
}
Run Code Online (Sandbox Code Playgroud)

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

只有当我连续扫描几次并且仅使用iPhone 4(没有使用4S冻结)时才会解除冻结

任何的想法 ?

干杯

只读存储器

End*_*ama 23

根据AV Cam View Controller示例调用startRunning或stopRunning在会话完成请求的操作之前不会返回.由于您将这些消息发送到主线程上的会话,因此会冻结所有UI,直到请求的操作完成.我建议您将调用包装在异步调度中,以便视图不会锁定.

- (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];
   }
} 
Run Code Online (Sandbox Code Playgroud)

  • 小心一点!`stopCapture`根据上面的代码操作视图层次结构.永远不要从GUI线程外部操纵GUI. (3认同)