项目地址:
https://github.com/yannickl/QRCodeReaderViewController
官方介绍的QRCode可以指定编码,特别是它支持ios7+,可以用来替换zxing、zbar。我在项目里一开始使用了zbar,一般时候它是正常的,但是扫描一个很小的二维码时,无法识别。切换为QRCode后,识别效果比较良好。
它会提供一个默认的view controller调用摄像头,同时会提供一个切换前置摄像头和后置摄像头的按钮。
这是官方截屏:
$ cd /path/to/MyProject $ touch Podfile $ vim Podfile source ‘https://github.com/CocoaPods/Specs.git‘ platform :ios, ‘7.0‘ pod ‘QRCodeReaderViewController‘, ‘~> 3.4.0‘然后在命令行执行:
// // ViewController.h // smarthome // // Created by 谢厂节 on 15/5/14. // Copyright (c) 2015年 WHR. All rights reserved. // #import <UIKit/UIKit.h> #import "QRCodeReaderViewController.h" @interface ViewController : UIViewController<QRCodeReaderDelegate> @end
.m文件里:
#import "QRCodeReaderViewController.h"
-(void)actionScan{ NSArray *types = @[AVMetadataObjectTypeQRCode]; QRCodeReaderViewController* _reader = [QRCodeReaderViewController readerWithMetadataObjectTypes:types]; // Set the presentation style _reader.modalPresentationStyle = UIModalPresentationFormSheet; // Using delegate methods _reader.delegate = self; // Or by using blocks [_reader setCompletionWithBlock:^(NSString *resultAsString) { [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"%@", resultAsString); }]; }]; [self presentViewController:_reader animated:YES completion:NULL]; } #pragma mark - QRCodeReader Delegate Methods - (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result { [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"%@", result); }]; } - (void)readerDidCancel:(QRCodeReaderViewController *)reader { [self dismissViewControllerAnimated:YES completion:NULL]; }使用CocoaPods安装时,可能编译会有错误,需要把
IOS8 二维码扫描-QRCodeReaderViewController
原文地址:http://blog.csdn.net/xundh/article/details/45919947