AVCaptureSession - 停止运行 - 需要很长很长一段时间(AVCaptureSes

2019-06-26 23:05发布

我使用斑马线为应用,这主要是比除了我允许扫描在连续几次的斑马线原来的代码相同的代码(即中,ZXingWidgetController不necesseraly只要检测到的东西解雇)。

我经历了很长很长冻结(有时它永远不会结束),当我按下按钮解雇调用

- (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无冻结)

任何的想法 ?

干杯

只读存储器

Answer 1:

按照AV凸轮视图控制器实例调用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];
   }
} 


文章来源: AVCaptureSession - Stop Running - take a long long time