我写了一个简单的UIViewController
,使用MTBBarcodeScanner
来扫描条形码。它完全适用于我的iPhone 6s +运行iOS 10,无论它是来自Xcode的开发版本还是来自 diawi.com的AdHoc发行版。不幸的是,它不适用于iPad(也运行iOS 10),当通过Xcode或AdHoc分发时。我的视图控制器正确滑动,按钮按预期工作,但预览窗口没有显示视频,也没有条形码被读取。
在此视图控制器加载之前,我已经检查了用户的权限。我从iOS获得了弹出窗口并在两个平台上授予它。 iPad的“设置”应用程序显示我确实可以访问相机。
这是我的代码。
#import "BarCodeScannerViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#import <MTBBarcodeScanner/MTBBarcodeScanner.h>
@interface BarCodeScannerViewController ()
@property (nonatomic, strong) MTBBarcodeScanner *scanner;
@end
@implementation BarCodeScannerViewController
- (void) loadView; {
[super loadView];
[self setTitle:@"Scan Bar Code"];
UIView *preview = [[UIView alloc] init];
[[self view] addSubview:preview];
[preview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self setScanner:[[MTBBarcodeScanner alloc] initWithMetadataObjectTypes:@[AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeQRCode] previewView:preview]];
UIView *buttonBar = [[UIView alloc] init];
[[self view] addSubview:buttonBar];
[buttonBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[buttonBar setBackgroundColor:[UIColor colorWithWhite:0 alpha:.5]];
UIButton *cancelButton = [[UIButton alloc] init];
[buttonBar addSubview:cancelButton];
[cancelButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
UIButton *torchToggle = nil;
if ([[self scanner] hasTorch]) {
UIButton *torchToggle = [[UIButton alloc] init];
[buttonBar addSubview:torchToggle];
[torchToggle setTranslatesAutoresizingMaskIntoConstraints:NO];
[torchToggle addTarget:[self scanner] action:@selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
[torchToggle setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[torchToggle setTitle:@"Toggle Light" forState:UIControlStateNormal];
}
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:preview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual
toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:44.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:buttonBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:[self view] attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeLeft multiplier:1.0 constant:5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
if ([[self scanner] hasTorch]) {
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:-5.0]];
[buttonBar addConstraint:[NSLayoutConstraint constraintWithItem:torchToggle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual
toItem:buttonBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5.0]];
}
[_scanner startScanningWithResultBlock:^(NSArray *codes) {
for (AVMetadataMachineReadableCodeObject *code in codes) {
if ([code stringValue]) {
[_scanner stopScanning];
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
if ([self barCodeScanned]) [self barCodeScanned]([code stringValue], self);
break;
}
}
} error:nil];
}
- (void) cancel;
{
[[self scanner] stopScanning];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
答案 0 :(得分:0)
感谢Mike Buss帮我解决这个问题。他告诉我,我可能太早开始使用相机。他建议我将startScanningWithResultBlock:
电话移至viewDidAppear:animated
。我用我的iPad进行了测试,现在效果很好。