AVCaptureSession - 停止运行 - 需要很长时间 [英] AVCaptureSession - Stop Running - take a long long time

查看:416
本文介绍了AVCaptureSession - 停止运行 - 需要很长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use ZXing for an app, this is mainly the same code than the ZXing original code except that I allow to scan several time in a row (ie., the ZXingWidgetController is not necesseraly dismissed as soon as something is detected).

当我按下解除按钮时,我会遇到长时间冻结(有时它永远不会结束)

I experience a long long freeze (sometimes it never ends) when I press the dismiss button that call

- (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是在委托方法内)

(please notice that the dismissModalViewController that remove the view is within the delegate method)

我只有在连续进行多次扫描时才会解除冻结,并且仅使用iPhone 4进行解冻(不冻结4S) )

I experience the freeze only while dismissing only if I made several scans in a row, and only with an iPhone 4 (no freeze with a 4S)

任何想法?

干杯

Rom

推荐答案

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

According to the AV Cam View Controller Example calling startRunning or stopRunning does not return until the session completes the requested operation. Since you are sending these messages to the session on the main thread, it freezes all the UI until the requested operation completes. What I would recommend is that you wrap your calls in an Asynchronous dispatch so that the view does not lock-up.

- (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];
   }
} 

这篇关于AVCaptureSession - 停止运行 - 需要很长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆