一 下载
项目地址:
https://github.com/yannickl/QRCodeReaderViewController
官方介绍的QRCode可以指定编码,特别是它支持ios7+,可以用来替换zxing、zbar。我在项目里一开始使用了zbar,一般时候它是正常的,但是扫描一个很小的二维码时,无法识别。切换为QRCode后,识别效果比较良好。
它会提供一个默认的view controller调用摄像头,同时会提供一个切换前置摄像头和后置摄像头的按钮。
这是官方截屏:
安装
推荐的安装方式是使用CocoaPods 包管理工具。
在Podfile文件里输入:
$ cd /path/to/MyProject $ touch Podfile $ vim Podfile source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'QRCodeReaderViewController', '~> 3.4.0'然后在命令行执行:
$ pod install
等待安装结束后,输入
open ****.xcworkspace
打开项目。
使用
头文件里:
//
// 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安装时,可能编译会有错误,需要把
Pods的Target修改一下设置:
这有另一种解决方式,我没有细看。放在这里作参考:
http://cameronspickert.com/2014/01/20/remove-the-arm64-architecture-from-cocoapods-targets.html