啥也不说,直接上源码,拷过去就能用。生成二维码的工具类使用方法在ProduceQRCode.h里有示例说明

分别将下面的ProduceQRCode.h和ProduceQRCode.m对应的代码考到自己创建.h和.m文件中即可。

ProduceQRCode.h文件源码

//
// ProduceQRCode.h
// iOS生成二维码图片
//
// Created by 刘成利 on 15/10/13.
// Copyright © 2015年 LiuChengLi. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> /**
* 二维码的中间添加小图片或logo:
*
由于生成的二维码被扫描时有一定的容错能力,成利记得是容错30%。
所以二维码中间带有小图片或logo的方法就是在已经生成好的二维码中间的上面再添加一张小的方形图片。
后期美工可以自己添加或合成上去。
*
*/ /**
* 生成二维码图片工具类的使用--示例: - (void)viewDidLoad {
[super viewDidLoad]; // 需要转换成二维码字符串
NSString *showCode = @"http://blog.csdn.net/it_liuchengli"; // 转换后的二维码图片
UIImage *img = [[ProduceQRCode alloc]initWithQRCodeString:showCode width:200].QRCodeImage; // 显示上面转换后的二维码图片 UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.center = self.view.center;
[self.view addSubview:imgView]; }
*/ @interface ProduceQRCode : NSObject // 生成的二维码图片在这里
@property (nonatomic, strong, readonly) UIImage *QRCodeImage; // 直接初始化调用此类alloc后的init如下的方法名
- (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width;
@end

ProduceQRCode.m文件中的代码

//
// ProduceQRCode.m
// iOS生成二维码图片
//
// Created by 刘成利 on 15/10/13.
// Copyright © 2015年 LiuChengLi. All rights reserved.
// #import "ProduceQRCode.h" @interface ProduceQRCode () @end @implementation ProduceQRCode - (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width{ self = [super init]; if (self)
{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [filter setDefaults]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data
forKey:@"inputMessage"]; CIImage *outputImage = [filter outputImage]; CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outputImage extent]]; UIImage *image = [UIImage imageWithCGImage:cgImage
scale:0.1
orientation:UIImageOrientationUp]; // 不失真的放大
UIImage *resized = [self resizeImage:image
withQuality:kCGInterpolationNone
rate:5.0]; // 缩放到固定的宽度(高度与宽度一致)
_QRCodeImage = [self scaleWithFixedWidth:width image:resized]; CGImageRelease(cgImage);
}
return self; } - (UIImage *)scaleWithFixedWidth:(CGFloat)width image:(UIImage *)image
{
float newHeight = image.size.height * (width / image.size.width);
CGSize size = CGSizeMake(width, newHeight);
UIGraphicsBeginImageContextWithOptions(size, NO, ); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0); CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage); UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageOut;
} - (UIImage *)resizeImage:(UIImage *)image
withQuality:(CGInterpolationQuality)quality
rate:(CGFloat)rate
{
UIImage *resized = nil;
CGFloat width = image.size.width * rate;
CGFloat height = image.size.height * rate; UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[image drawInRect:CGRectMake(, , width, height)];
resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return resized;
} @end

最新文章

  1. Spring(四)注解配置Ioc
  2. 【原】iOS容易造成循环引用的三种场景,就在你我身边!
  3. [Android Pro] proguard.cfg 配置文件
  4. MIME对应表
  5. iOS人脸识别(CoreImage)
  6. Tcp 数据对象传输接口对象设计
  7. RobotFramework 数据分离
  8. window.onload 和 $(document).ready()
  9. linux下安装多个Tomcat
  10. m个小朋友手拉手站成一个圈,从第k个小朋友开始报数,报到n的那个小朋友退到圈外,然后他的下一位重新报“1”。这样继续下去,直到只剩下一个小朋友,求解这个小朋友原来站什么地方。
  11. java实现爬虫功能
  12. js 布局转换问题
  13. vue实战之狗血事件:页面loading效果诡异之事
  14. 【转】【Python】Python 中文编码报错
  15. MySQL集群Percona XtraDB Cluster安装搭建步骤详解
  16. GreenPlum学习笔记:新手入门命令
  17. nginx重启命令方法(linux,centos,ubuntu)总结
  18. UNITY技巧-查找脚本被哪个场景引用
  19. GitHub 配置指南
  20. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs &amp;&amp; 离线 &amp;&amp; 并查集)

热门文章

  1. localStorage保存账号密码
  2. [NOIP2000]方格取数
  3. 【JS】Beginner7:Functions
  4. Java JDK8 安装及环境变量配置
  5. nodejs调试工具node-inspector入门随笔
  6. Spring-demo1(初学者的尝试,2015.03.19)
  7. Java 中 String 类和StringBuilder 类的常用方法
  8. [置顶] Oracle job procedure 存储过程定时任务
  9. C#下的 Emgu CV
  10. SIGGRAPH