用CIFilter生成QRCode二维码图片

CIFilter不仅仅可以用来做滤镜,它还可以用来生成二维码.

CIFilterEffect.h + CIFilterEffect.m

//
// CIFilterEffect.h
// CIFilter
//
// Created by YouXianMing on 14-5-9.
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> /* CILinearToSRGBToneCurve
CIPhotoEffectChrome
CIPhotoEffectFade
CIPhotoEffectInstant
CIPhotoEffectMono
CIPhotoEffectNoir
CIPhotoEffectProcess
CIPhotoEffectTonal
CIPhotoEffectTransfer
CISRGBToneCurveToLinear
CIVignetteEffect */ @interface CIFilterEffect : NSObject @property (nonatomic, strong, readonly) UIImage *filterImage;
- (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name; @property (nonatomic, strong, readonly) UIImage *QRCodeImage;
- (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width; @end
//
// CIFilterEffect.m
// CIFilter
//
// Created by YouXianMing on 14-5-9.
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "CIFilterEffect.h" @interface CIFilterEffect () @property (nonatomic, strong, readwrite) UIImage *filterImage;
@property (nonatomic, strong, readwrite) UIImage *QRCodeImage; @end @implementation CIFilterEffect - (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name
{
self = [super init];
if (self)
{
// 将UIImage转换成CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:image]; // 创建滤镜
CIFilter *filter = [CIFilter filterWithName:name
keysAndValues:kCIInputImageKey, ciImage, nil];
[filter setDefaults]; // 获取绘制上下文
CIContext *context = [CIContext contextWithOptions:nil]; // 渲染并输出CIImage
CIImage *outputImage = [filter outputImage]; // 创建CGImage句柄
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:[outputImage extent]]; _filterImage = [UIImage imageWithCGImage:cgImage]; // 释放CGImage句柄
CGImageRelease(cgImage);
}
return self;
} - (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. 带中文的路径导致NSURL初始化一直为null的问题
  2. #undef
  3. LayoutInflater中四种类型inflate方法的介绍
  4. 如何优雅的实现界面跳转 之 统跳协议 - DarwinNativeRouter
  5. IOS SWIFT UITableView 实现简单微博列表
  6. 一起C语言中程序时序问题的排查过程
  7. Hibernate核心接口
  8. devstack安装使用openstack常见问题与解决的方法
  9. 6、plsql编程
  10. C# List 扩展排序
  11. 全景智慧城市——NOW!!!VRSHOPPING颠覆你的购物认知!
  12. Netty4.0.24.Final 版本中 IdleStateHandler 使用时的局限性
  13. 《Linux Device Drivers》第十八章 TTY驱动程序——note
  14. 实战经验分享之C#对象XML序列化
  15. centos后台运行Python
  16. MATLAB——神经网络sim仿真函数
  17. jvm的调优
  18. MongoVUE的Collections数据不显示的解决方法
  19. Nodejs学习笔记(十)—与MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
  20. HDUOJ----2159 FATE

热门文章

  1. mojing SDK根据坐标进行移动
  2. Python时间calender模块介绍
  3. Kafka 0.9 新消费者API
  4. c++ 网络编程(四) LINUX/windows下 socket 基于I/O复用的服务器端代码 解决多进程服务端创建进程资源浪费问题
  5. hibernate抓取问题
  6. sqlplus column命令用法
  7. hibernate 学习笔记2
  8. rest webapi 返回数据
  9. RabbitMQ小节
  10. 预防XSS方法:HtmlEncode和JavaScriptEncode(转)