用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

看看以下使用情况,一行代码搞定!

以下几个二维码,闲得无聊可以扫一扫......

用CIFilter生成QRCode二维码图片的更多相关文章

  1. PHP生成QRCode二维码

    php生成QRCode二维码示例 <?php //引入 phpqrcode 类库 //phpqrcode下载地址:https://github.com/t0k4rt/phpqrcode //或从 ...

  2. jQuery生成QRcode二维码

    jQuery生成QRcode二维码示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  3. Java使用ZXing生成/解析二维码图片

    ZXing是一种开源的多格式1D/2D条形码图像处理库,在Java中的实现.重点是在手机上使用内置摄像头来扫描和解码设备上的条码,而不与服务器通信.然而,该项目也可以用于对桌面和服务器上的条形码进行编 ...

  4. 微信支付-无法识别qrcode生成的二维码图片

    1.开始使用 table方式,但是还是无法识别二维码  http://www.cnblogs.com/staticed/p/8549316.html var code_url = data.code_ ...

  5. JAVA生成二维码图片代码

    首先需要导入 QRCode.jar 包 下载地址看这里   http://pan.baidu.com/s/1o6qRFqM import java.awt.Color;import java.awt. ...

  6. java--实现将文字生成二维码图片,并在中间附上logo,下方附上文字

    前段时间因为工作需要,要实现将一段文字或者url生成二维码,然后中间附上logo,下方正中间附上文字的功能. 上网找了几篇教程学习了下,由于没有保存借鉴的博文链接,所以就没po上参考文章的链接啦,感谢 ...

  7. asp.net.web如何简单生成和保存二维码图片的例子

    首先,要有生成二维码图片,需要二维码生成的类库,到官网下载thoughtWorks.QRCode.dll 例子的步骤: 1.创建项目QRCodeTest1,选择asp.net.web窗体应用程序

  8. 使用javascript生成当前博文地址的二维码图片

    前面的话 在电脑端发现一篇好的博文,想在手机上访问.这时,就必须打开手机浏览器输入长长的URL地址才行,非常不方便.如果在博客标题的后面跟一张小的图片,点击该图片后,出现一张二维码的大图,然后再通过手 ...

  9. c# 生成二维码图片

    转载自:https://blog.csdn.net/hyunbar/article/details/78271778 1.在C#中直接引用ThoughtWorks.QRCode.dll 类 2.封装方 ...

随机推荐

  1. C# DataTable 用法

    1.创建DataTable DataTable dataTable = new DataTable(); //创建一个空表 2.创建DataRow DataRow row = dataTable.Ne ...

  2. Axure原型设计介绍

    在第八周的课堂上,王文娟老师在校园系统上发布了对于自行选择的原型设计软件进行资料查找以及自学的任务.因为之前的课程学习需要,我们大概掌握了原型设计软件Axure的使用,下面是一些我们学习过程中的介绍 ...

  3. 爬虫--requeste

    1.requeste模块,是我们Python对我们爬虫有好的一面,,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的地位.没有的话直接pip3 install  ...

  4. apache2 + django 路径问题

    问题: 在代码中使用sys.path.append(), 添加模块路径后,仍然报错找不到包. 虽然在LD_LIBRARY_PATH中配置了.so文件打路径,仍然报错找不到. 原因: 检查apahce2 ...

  5. clojure学习笔记(一)

    下载地址 需要安装xmind打开 http://pan.baidu.com/s/1dDxKj1B

  6. js需要清楚的内存模型

    原型 原型重写 原型继承 组合方式实现继承 函数作用域链

  7. unity调用ios原生代码objective-c和回调

    从c#到objective-c学习 https://www.runoob.com/w3cnote/objective-c-tutorial.html https://www.jianshu.com/p ...

  8. 关于重绘and重排

    在研究CSS3动画性能的时候,看到了重排两个字. 突然想到自己虽然听说过这么个东东,但一直也没深入研究之. 趁着当下正好有研究的劲头,所以一不做二不休,把这个point也给学习了. 同样是一番查找资料 ...

  9. sql语句将身份证号数字转换成特殊字符

    SELECT Tname , STUFF(Idcard,,,'*********') as Idcard,Completion from demo

  10. Java复习第二天

    Day04 1.switch语句的格式?针对格式的解释?以及注意事项? (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ...