二维码扫描/生成框架GLScanner使用详解 CocoaPo
GLScanner 最好用的iOS二维码、条形码,扫描、生成框架,支持闪光灯,从相册获取;个性化修改,错误处理回调。项目地址:GLScanner
1、一个方法初始化所有,Block回调,简单方便
2、支持个性化修改UI
3、支持多语言设置(英语、简体中文、繁体中文),或者自己设置所有语言
1、在使用的地方导入#import <GLScanner.h>
, 调用二维码扫描视图:
Xcode8.0以上要求在使用相机或者相册等,项目
info.plist
要添加Privacy - Photo Library Usage Description
和Privacy - Camera Usage Description
键值对,不然不给访问,不然不给访问,不然不给访问。千万别忘记了!!!!!!!!
GLScannerController *scanner = [GLScannerController scannerWithInitRootView:^(GLScannerViewController *rootScannerView) {
#pragma mark UI初始化个性修改
} completion:^(NSString *value, BOOL *dismissAnimation) {
#pragma mark 扫描成功处理
} error:^(GLScannerViewController *rootScannerView, NSError *error) {
#pragma mark 错误处理
}];
[self presentViewController:scanner animated:true completion:nil];
2、UI初始化个性修改参数详解:
//标题颜色
rootScannerView.titleColor = [UIColor whiteColor];
//设置bar背景图片或者颜色 二选一 优先图片
rootScannerView.barBackgroundImage = [UIImage imageNamed:@"bar_background"];
//rootScannerView.barBackgroundTintColor = [UIColor lightGrayColor];
//统一设置bar 和扫描工作的图片的颜色
rootScannerView.tintColor = [UIColor whiteColor];
//设置遮挡颜色
//rootScannerView.coverColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
//自定义左侧item
rootScannerView.leftBarButtonItem = ^UIButton *(UIButton *leftButton) {
//可以修改button的一些属性
//自己创建一个返回也可以 可以自己实现触发方法
return leftButton;
};
//自定义右侧item
rootScannerView.rightBarButtonItem = ^UIButton *(UIButton *rightButton) {
//可以修改button的一些属性
[rightButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//优先这里设置
[rightButton setTitle:@"Photo" forState:UIControlStateNormal];
//自己创建一个返回也可以
return rightButton;
};
//可以隐藏右侧ltem
rootScannerView.hiddenRightBarButtonItem = true;
//隐藏提示label
rootScannerView.hiddenTipLabel = true;
多语言设置:可以根据你个人应用需求设置语言,languageCode
属性决定使用什么语言,值是一个静态变量,具体看GLScannerConfig.h
文件,例:
rootScannerView.languageCode = GLScannerLocalizedStringZh_Hans;
语言默认跟随系统,也可以自己指定赋值,优先这里设置的语言,如:
//或者设置语言文本
rootScannerView.textStringDic[GLScannerBarTitle] = @"掃一掃";
rootScannerView.textStringDic[GLScannerBarRightTitle] = @"Photo";
rootScannerView.textStringDic[GLScannerDefaultTipContent] = @"将二维码放入框中,即可自动扫描";
3、完成回调:
回调参数 BOOL *dismissAnimation
是设置扫描导航控制器消失是否动画,记住是一个指针。赋值方式:
*dismissAnimation = false;
4、错误处理
具体看文件GLScannerError.h
的枚举GLSimpleScannerErrorCode
内容,自己根据回调的错误的code做一些处理。
比如相机或相册拒绝授权,可以调用:
//跳转该应用的设置url
[GLQrScanner openSettingsURLString];
5、二维码生成和图片识别二维码
可以设置icon与二维码大小的比例,默认0.2
NSString *strng = @"https://github.com/Gavin-ldh";
UIImage *iconImage = [UIImage imageNamed:@"icon"];
CGFloat width = self.view.frame.size.width/5.0;
iconImage = [iconImage imageByResizeToSize:CGSizeMake(width, width) contentMode:UIViewContentModeScaleAspectFill];
[GLQrScanner qrImageWithString:strng icon:iconImage scale:0.15 completion:^(UIImage *image) {
self.imageView.image = image;
}];
识别图片二维码调用如下:
[GLQrScanner scaneImage:image completion:^(NSArray *values) {
}];
如果页面UI跟我的界面差距很大,也可以根据GLQrScanner.h
的头文件调用二维码识别方法。具体注释都很全面,可以下载看下。下载地址:GLScanner
6、安装使用
CocoaPods安装:pod 'GLScanner'
如果搜索不到,请先运行
rm ~/Library/Caches/CocoaPods/search_index.json
在搜索。
下载ZIP包,将GLScanner资源文件拖到工程中。
使用介绍就这些了,都是很简单的,希望对大家有用,大家多多支持。如果觉得有用请给个Star-GLScanner,在这里万分感谢。