我正在尝试使用新的 iOS 7 功能实现 QRCode 扫描仪,但我的代码没有调用 mainAVCaptureMetadataOutputObjectsDelegate
方法。
我之前使用过 AVFoundation 相机,并且在我当前的实现中,我已经让预览层运行没有问题。甚至将我的输出切换回AVCaptureVideoDataOutput
验证我的会话设置。
我使用这个NSHipster 帖子作为指导,这是我到目前为止的代码:
界面:
@import AVFoundation;
@interface QRCodeViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property (strong, nonatomic) AVCaptureDevice* device;
@property (strong, nonatomic) AVCaptureDeviceInput* input;
@property (strong, nonatomic) AVCaptureMetadataOutput* output;
@property (strong, nonatomic) AVCaptureSession* session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer* preview;
@end
设置:
- (void)setupCamera
{
// Device
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// Input
self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
// Output
self.output = [[AVCaptureMetadataOutput alloc] init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
// Session
self.session = [[AVCaptureSession alloc] init];
[self.session addInput:self.input];
[self.session addOutput:self.output];
// Preview
self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.layer insertSublayer:self.preview atIndex:0];
// Start
[self.session startRunning];
}
委托方式:
// DELEGATE METHOD NOT CALLED
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
NSLog(@"Metadata");
}
任何帮助是极大的赞赏!