我試圖用新的iOS 7功能實現QRCode掃描器,但我的代碼並沒有調用主要的AVCaptureMetadataOutputObjectsDelegate
方法。iOS 7 AVCaptureMetadataOutput委託(QRCode掃描器)
我已經使用AVFoundation相機之前和我目前的實施我已預覽層運行沒有問題。即使將我的輸出切換回AVCaptureVideoDataOutput
也驗證了我的會話設置。
我使用這個NSHipster post作爲一個準則,這是我到目前爲止的代碼:
接口:
@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");
}
任何幫助,非常感謝!
布拉沃先生!成爲如此快速的自學者的額外榮譽。我預計很多開發人員一旦更多地參與新的iOS 7功能,就會發現您的答案非常有用。 –
一旦我想到它就很有意義 - 直到元數據對象與設備關聯後,元數據顯然無法知道。 –
我把我的小樣本項目放在github上,如果有人感興趣:https://github.com/kpmiller/ios7-barcode –