【问题标题】:iOS: AVCaptureOutput stops working after navigationiOS:AVCaptureOutput 在导航后停止工作
【发布时间】:2015-06-17 13:14:51
【问题描述】:

我将相机视图用作条形码扫描仪,它扫描条形码并使用 webview 将 segue 启动到另一个视图控制器。这工作正常,我可以从 web 视图导航回扫描仪并毫无问题地扫描另一个条形码。但是,如果我使用相机离开视图控制器并返回到它,相机视图会加载但不再检测条形码。

@implementation ProductScanViewController

NSString *loadUrl;     
AVCaptureSession *_captureSession;
AVCaptureDevice *_videoDevice;
AVCaptureDeviceInput *_videoInput;
AVCaptureVideoPreviewLayer *_previewLayer;
BOOL _running;
AVCaptureMetadataOutput *_metadataOutput;
@synthesize mWebView;


- (void)viewDidLoad {
    [super viewDidLoad];
NSURL *url = [NSURL URLWithString:loadUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {if([data length] > 0 && error == nil)[mWebView loadRequest:request];         else if (error != nil) NSLog(@"Error: %", error);}
 ];

    [self setupCaptureSession];
}

- (void)setupCaptureSession {

    // 1
    if (_captureSession){        
        return;
    }

    // 2
    _videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (!_videoDevice) {
        return;
    }

    // 3
    _captureSession = [[AVCaptureSession alloc] init];

    // 4
    _videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_videoDevice error:nil];

    // 5
    if ([_captureSession canAddInput:_videoInput]) {
        [_captureSession addInput:_videoInput];
    }

    // 6
    _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    // capture and process the metadata
    _metadataOutput = [[AVCaptureMetadataOutput alloc] init];
    dispatch_queue_t metadataQueue =
    dispatch_queue_create("com.1337labz.featurebuild.metadata", 0);
    [_metadataOutput setMetadataObjectsDelegate:self
                                      queue:metadataQueue];
    if ([_captureSession canAddOutput:_metadataOutput]) {
        [_captureSession addOutput:_metadataOutput];
    }
}

#pragma mark - Delegate functions

- (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputMetadataObjects:(NSArray *)metadataObjects
       fromConnection:(AVCaptureConnection *)connection {

    [metadataObjects enumerateObjectsUsingBlock:^(AVMetadataObject *obj,
                               NSUInteger idx,
                              BOOL *stop) {
         if ([obj isKindOfClass: [AVMetadataMachineReadableCodeObject class]]) {
             //NSLog(@"Capture Output started");

            // 3
            AVMetadataMachineReadableCodeObject *code = (AVMetadataMachineReadableCodeObject*)
            [_previewLayer transformedMetadataObjectForMetadataObject:obj];

            // 4
            Barcode * barcode = [Barcode processMetadataObject:code];

            for (NSString * str in self.allowedBarcodeTypes) {
                 if([barcode.getBarcodeType isEqualToString:str]){
                     [self validBarcodeFound:(barcode)];
                     return;
                 }
             }
         }
    }];
}

- (void) validBarcodeFound:(Barcode *)barcode{
NSLog(@"Found Barcode");

[self stopRunning];
[self.foundBarcodes addObject:barcode];
//[self showBarcodeAlert:barcode];
NSString *alertMessage = @"";
alertMessage = [alertMessage stringByAppendingString:[barcode getBarcodeType]];
NSLog([barcode getBarcodeData]);
NSLog(alertMessage);

NSLog([NSString stringWithFormat:@"%@", barcode.getBarcodeData]);
if ([barcode.getBarcodeType isEqualToString:@"org.iso.QRCode"])
{
 if ([[NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1] length] > 0){

    NSString *input = [barcode getBarcodeData];
    [NSString stringWithFormat:@"%lu",(unsigned long)[self.foundBarcodes count]-1];
    NSLog(input);


    if ([input length] >= 13)
    {
        input = [input substringToIndex:12];
    }
    loadUrl = [[@"http://www.mywebsite.co.uk/" stringByAppendingString:input] stringByAppendingString:@"?utm_source=iphone"];
    NSLog(loadUrl);        
    dispatch_sync(dispatch_get_main_queue(), ^{
        [self performSegueWithIdentifier:@"toWebView" sender:self];
    });
}


}

【问题讨论】:

  • 什么时候发现条形码?你是做什么的?
  • 您是否正在将您的视图控制器导航到另一个视图控制器?
  • 是的,它向嵌入在另一个视图控制器中的 webview 启动 segue。
  • 当你回来时它会为你的对象清零,检查所有对象。
  • 我可以往返于 webview 而不会造成问题。只有当我从条形码扫描仪返回堆栈时,它才会停止工作。

标签: ios objective-c avcapturesession avcaptureoutput


【解决方案1】:

所以我设法解决了这个问题,但无法真正理解它的工作原理。

我没有在 viewDidLoad 期间异步加载 URL,而是使用 segue 传递 URL,并从包含 WebView 的 ViewController 中加载它。

除此之外,变量声明被封装在大括号{} 中,@synthesize mWebView 被删除。我不知道为什么会导致问题,所以任何可能的解释都将不胜感激

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多