[英]Why isn't the camera working on my iPad with MTBBarcodeScanner?
我编写了一个简单的UIViewController
,它使用MTBBarcodeScanner
扫描条形码。 无论是Xcode的开发版本还是diawi.com的AdHoc发行版,它都能在运行iOS 10的iPhone 6s +上完美运行。 不幸的是,通过Xcode或AdHoc分发时,它不能在iPad(也运行iOS 10)上运行。 我的视图控制器正确滑动,按钮按预期工作,但预览窗口显示无视频,也没有条形码被读取。
在加载此视图控制器之前,我已经检查了用户的权限。 我从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
感谢Mike Buss为我提供帮助。 他告诉我,我可能开相机太早。 他建议我将startScanningWithResultBlock:
调用移至viewDidAppear:animated
。 我用iPad进行了测试,现在效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.