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

标签 iphone ios zxing avcapturesession

我将 ZXing 用于应用程序,这主要是与 ZX​​ing 原始代码相同的代码,除了我允许连续扫描几次(即,ZXingWidgetController 不必在检测到某些内容时立即关闭)。

当我按下调用的关闭按钮时,我遇到了长时间的卡住(有时它永远不会结束)

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

(请注意删除 View 的 dismissModalViewController 在委托(delegate)方法中)

只有当我连续进行多次扫描并且只有 iPhone 4(4S 没有卡住)时,我才会在解雇时遇到卡住

有什么想法吗?

干杯

只读

最佳答案

根据AV Cam View Controller Example在 session 完成请求的操作之前,调用 startRunning 或 stopRunning 不会返回。由于您将这些消息发送到主线程上的 session ,它会卡住所有 UI,直到请求的操作完成。我建议您将调用包装在异步调度中,这样 View 就不会锁定。

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

关于iphone - AVCaptureSession - 停止运行 - 需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905505/

相关文章:

iphone - 我对 UIKit 中的框架和边界有正确的理解吗?

iphone - iOS - 强制键盘以横向显示

iphone - 未调用 UIViewController 准备 segue 方法

android: zxing 条形码扫描成功但未从 Activity 返回

zxing 返回 CODE_39-Barcode 的错误位置

iphone - 如何使 UISegmentedControl 的选定部分变暗?

ios - 在 Swift 中,您如何检测哪些 UIControlEvents 触发了操作?

ios - 单击 tableviewcell 特定的 Firebase 数据后如何接收到 viewcontroller?

ios - Swift/Xcode:如何从一个新的.swift文件中的另一个函数中调用一个函数?

java - Zxing - UPC+5补充条码检测