【问题标题】:Obj. C - QR Reading application runs too slow对象。 C - QR 阅读应用程序运行速度太慢
【发布时间】:2015-08-30 09:56:46
【问题描述】:

我通过this site 上的教程(Appcoda 教程)使用 AVFoundation 制作了一个二维码阅读应用程序。读取二维码后,应用显示 UIAlertView。但它需要将近 2 分钟(有时超过 3 分钟)。我将在此处粘贴整个 ViewController.m 文件。我希望这已经足够了。 (UIAlertView 在 captureOutput 方法中)

//
//  ViewController.m
//  Yuvio
//
//  Created by İhsan Batuğhan YILMAZ on 29/08/15.
//  Copyright © 2015 Farabius. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>


@interface ViewController ()

@property (nonatomic) BOOL isReading;

@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

-(BOOL)startReading;
-(void) stopReading;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _isReading=NO;
    _captureSession = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)startStopReading:(id)sender {
    if (!_isReading) {
        if ([self startReading]) {
            [_btnStart setTitle:@"Stop"];
        }
    }
    else{
        [self stopReading];
        [_btnStart setTitle:@"Start!"];
    }

    _isReading = !_isReading;
}
- (BOOL)startReading {
    NSError *error;

    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }

    _captureSession = [[AVCaptureSession alloc] init];
    [_captureSession addInput:input];

    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    [_captureSession addOutput:captureMetadataOutput];

    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    [_videoPreviewLayer setFrame:_cameraView.layer.bounds];
    [_cameraView.layer addSublayer:_videoPreviewLayer];

    [_captureSession startRunning];

    return YES;
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {

            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"QR Detected"
                                  message:[metadataObj stringValue]
                                  delegate:self
                                  cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];

            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
            [_btnStart performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
            _isReading = NO;
        }
    }
}
-(void)stopReading{
    [_captureSession stopRunning];
    _captureSession = nil;

    [_videoPreviewLayer removeFromSuperlayer];
    return;
}

@end

【问题讨论】:

    标签: ios objective-c avfoundation uialertview qr-code


    【解决方案1】:

    我认为问题在于在主线程之外使用 UI 函数。试试这个代码:

    -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
        if (metadataObjects != nil && [metadataObjects count] > 0) {
            AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
            if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
                __weak ViewController *weakSelf = self;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf processQRCode:metadataObj];
                });
            }
        }
    }
    
    -(void)processQRCode:(AVMetadataMachineReadableCodeObject *)codeObject{
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"QR Detected"
                              message:[codeObject stringValue]
                              delegate:self
                              cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    
        [self stopReading];
        [_btnStart setTitle:@"Start!" forState:UIControlStateNormal];
        _isReading = NO;
    }
    

    我用这个修复检查了你的视图控制器,它运行得很快。

    【讨论】:

    • 如果您认为问题已解决,您可以将答案标记为正确。
    • 您的回答在单视图应用程序中对我有用。再次感谢。但是我如何在选项卡式应用程序中使用它。因为似乎 _weak ViewController 不适用于选项卡式视图应用程序。
    • 好吧我找到了,直接删除_weak ViewController *weakSelf = self;并在调用方法时更改使用 self 而不是 weakSelf。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多