在集成掃描二維碼功能時候,我使用的是系統在iOS7.0 以後才支持的掃描二維碼功能類。剛開始建立代碼是這麼寫的html
1 -(void)setUpCamera 2 { 3 self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 4 5 self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; 6 7 self.outPut = [[AVCaptureMetadataOutput alloc]init]; 8 self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; 9 [_outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; 10 self.session = [[AVCaptureSession alloc]init]; 11 [self.session setSessionPreset:AVCaptureSessionPresetHigh]; 12 if ([self.session canAddInput:self.input]) 13 { 14 [self.session addInput:self.input]; 15 16 } 17 18 if ([self.session canAddOutput:self.outPut]) 19 { 20 [self.session addOutput:self.outPut]; 21 } 22 23 24 _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session]; 25 _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; 26 _preview.frame =CGRectMake(20,110,280,280); 27 [self.view.layer insertSublayer:self.preview atIndex:0]; 28 29 [self.session startRunning]; 30 }
在其代理類裏面ios
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *stringValue; if ([metadataObjects count] > 0 ) { AVMetadataMachineReadableCodeObject *metadataObject = [metadataObjects objectAtIndex:0]; stringValue = metadataObject.stringValue; } [self.session stopRunning]; [self dismissViewControllerAnimated:YES completion:^{ [timer invalidate]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"二維碼相關內容" message:stringValue delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil]; [alertView show]; }]; }
在真機上測試,系統是iOS7.1.1session
運行時出現下面的錯誤: app
*** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] – unsupported type found. Use -availableMetadataObjectTypes.’ide
解決不了,上網上搜索也沒有相關的解決方法,最後查找相關文檔而且查看手冊,仍是不可以解決問題,請教一大神,查看系統幫助文檔的 AVMetadataObjectTypes後改變了建立代碼塊-(void)setUpCamera以下;測試
-(void)setUpCamera { self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; self.outPut = [[AVCaptureMetadataOutput alloc]init]; // self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; // self.outPut.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; [_outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; self.session = [[AVCaptureSession alloc]init]; [self.session setSessionPreset:AVCaptureSessionPresetHigh]; if ([self.session canAddInput:self.input]) { [self.session addInput:self.input]; } if ([self.session canAddOutput:self.outPut]) { [self.session addOutput:self.outPut]; } self.outPut.metadataObjectTypes = [NSArray arrayWithObject:AVMetadataObjectTypeQRCode]; _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; _preview.frame =CGRectMake(20,110,280,280); [self.view.layer insertSublayer:self.preview atIndex:0]; [self.session startRunning]; }
這樣問題就解決了spa
大神解釋官方文檔的意思大體是:代理
AVCaptureMetadataOutput類的對象(在本例裏是self.outPut),self.outPut的屬性metadataObjectTypes要在AVCaptureSession類對象(本例爲self.session)code
[self.session addOutput:self.outPut]即添加了AVCaptureMetadataOutput的對象後,方可對其進行設置;xml