标签栏中的ZXingObjc条形码扫描仪会抛出错误

时间:2014-01-16 02:23:26

标签: ios barcode-scanner tabbar

我的应用程序有一个标签栏和导航控制器。无论什么时候我点击扫描选项卡,它都会在线上抛出线程1:EXC_BAD_ACCESS错误:

ZXCapture.m

[output ZXQT(setDelegate:)ZXAV(setSampleBufferDelegate:)self

这是我的代码: 标签栏:

TestScanViewController *scannerViewController=[[TestScanViewController alloc] initWithNibName:@"TestScanViewController" bundle:nil];
navigationController =[[UINavigationController alloc] initWithRootViewController:scannerViewController];
TestScanViewController.h中的

@interface TestScanViewController : UIViewController <ZXCaptureDelegate>
@end
TestScanViewController.m中的

#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import "TestScanViewController.h"

@interface TestScanViewController ()
@property (nonatomic, strong) ZXCapture* capture;
@property (nonatomic, weak) IBOutlet UILabel* decodedLabel;

@end

@implementation TestScanViewController


#pragma mark - View Controller Methods

- (void)viewDidLoad {
[super viewDidLoad];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    NSLog(@"%s",__PRETTY_FUNCTION__);

    if (self.capture == nil) {
        self.capture = [[ZXCapture alloc] init];
        self.capture.delegate = self;
        self.capture.rotation = 90.0f;

        // Use the back camera
        self.capture.camera = self.capture.back;
        self.capture.layer.frame = self.view.bounds;;
        [self.view.layer addSublayer:self.capture.layer];

        [self.view bringSubviewToFront:self.decodedLabel];
    }else{
        [self.capture start];
        [self.view.layer addSublayer:self.capture.layer];
    }

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"%s",__PRETTY_FUNCTION__);

    [self.capture.layer removeFromSuperlayer];
    [self.capture stop];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark - Private Methods

- (NSString*)displayForResult:(ZXResult*)result {
    }

    return [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@", formatString, result.text];
}

#pragma mark - ZXCaptureDelegate Methods

- (void)captureResult:(ZXCapture*)capture result:(ZXResult*)result {
    if (result) {
        // We got a result. Display information about the result onscreen.
        [self.decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:[self displayForResult:result] waitUntilDone:YES];

        // Vibrate
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}

- (void)captureSize:(ZXCapture*)capture width:(NSNumber*)width height:(NSNumber*)height {
}

@end

就像github中的示例代码一样: https://github.com/TheLevelUp/ZXingObjC/blob/master/examples/BarcodeScanner/ViewController.m

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。如果您的目标是iOS版本6或更高版本并使用ARC,则问题的原因是在ZXCapture.m中未完全迁移到ARC。

查看第56行的代码:

@interface ZXCapture ()

@property (nonatomic, assign) dispatch_queue_t captureQueue;

@end

assign是ARC日之前的遗留物。它还会导致编译器警告,这是解决方案的线索。 要解决此问题,请将代码更改为:

@interface ZXCapture ()

@property (nonatomic, strong) dispatch_queue_t captureQueue;

@end