iOS 二维码
基本上我们做二维码的实现方法有三大途径:ZBar、ZXing、AVFoundation.
第一种:ZBar
1.下载ZBarSDK,并且导入到工程里面
2.导入依赖库:
QuartzCore.framework、CoreVideo.framework、CoreMedia.framework、AVFoundation.framework
3.如果报does not contain bitcode,请点击这里
4.Zbar有两种方式:
1>方式是常规的方式,通过ZBarReaderViewController直接打开一个扫描界面进行扫描。
首先引用头文件 #import"ZBarSDK.h",实现代理<ZBarReaderDelegate>
//初始化扫描二维码控制器
ZBarReaderViewController *reader = [ZBarReaderViewController new];
//设置代理
reader.readerDelegate = self;
//二维码/条形码识别设置
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
//弹出系统照相机,全屏拍摄
[self presentViewController:reader animated:YES completion:nil];
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol * symbol;
for(symbol in results)
break;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
self.label.text = symbol.data;
注:symbol.data就是扫描二维码之后的值,[info objectForKey:UIImagePickerControllerOriginalImage]是扫描二维码之后的图片
}
2>方式是自定义扫描界面的方式。即通过自定义ZBarReaderView的界面达到自定义的效果。
首先引用头文件 #import"ZBarSDK.h",实现代理<ZBarReaderViewDelegate>
初始化扫描二维码对象ZBarReaderView
_readview = [[ZBarReaderView alloc] init];
//设置扫描代理
_readview.readerDelegate = self;
//关闭闪光灯
_readview.torchMode = 0;
//显示帧率
_readview.showsFPS = NO;
//将其照相机拍摄视图添加到要显示的视图上
[self.view addSubview:_readview];
//二维码/条形码识别设置
ZBarImageScanner *scanner = _readview.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
//扫描二维码的时候,识别成功会进入此方法,读取二维码内容
- (void) readerView: (ZBarReaderView*) readerView didReadSymbols: (ZBarSymbolSet*) symbols fromImage: (UIImage*) image
{
ZBarSymbol * symbol =nil;
for(symbol in symbols)
break;
注:symbol.data就是扫描二维码之后的值,image是扫描二维码之后的图片
}
当生成二维码的时候:
首先导入libqrencode这个框架,将#import "QRCodeGenerator.h"导进来
返回的image = [QRCodeGenerator qrImageForString:@''字符串"imageSize:testImageView.bounds.size.width];
当然也可以进行保存到相册
//保存二维码图片到相册
-(void)saveImageToAlbum
{
UIImageWriteToSavedPhotosAlbum(返回的image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
//保存图片回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void *)contextInfo
{
if(error != NULL)
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请打开应用的相册权限" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alertVC addAction:action];
[self presentViewController:alertVC animated:YES completion:^{
}];
}
else
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"已保存到相册" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
[alertVC addAction:action];
[self presentViewController:alertVC animated:YES completion:^{
}];
}
}