用CIFilter生成QRCode二维码图片

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

CIFilterEffect.h + CIFilterEffect.m

  1. //
  2. // CIFilterEffect.h
  3. // CIFilter
  4. //
  5. // Created by YouXianMing on 14-5-9.
  6. // Copyright (c) 2014年 Y.X. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. /*
  12.  
  13. CILinearToSRGBToneCurve
  14. CIPhotoEffectChrome
  15. CIPhotoEffectFade
  16. CIPhotoEffectInstant
  17. CIPhotoEffectMono
  18. CIPhotoEffectNoir
  19. CIPhotoEffectProcess
  20. CIPhotoEffectTonal
  21. CIPhotoEffectTransfer
  22. CISRGBToneCurveToLinear
  23. CIVignetteEffect
  24.  
  25. */
  26.  
  27. @interface CIFilterEffect : NSObject
  28.  
  29. @property (nonatomic, strong, readonly) UIImage *filterImage;
  30. - (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name;
  31.  
  32. @property (nonatomic, strong, readonly) UIImage *QRCodeImage;
  33. - (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width;
  34.  
  35. @end
  1. //
  2. // CIFilterEffect.m
  3. // CIFilter
  4. //
  5. // Created by YouXianMing on 14-5-9.
  6. // Copyright (c) 2014年 Y.X. All rights reserved.
  7. //
  8.  
  9. #import "CIFilterEffect.h"
  10.  
  11. @interface CIFilterEffect ()
  12.  
  13. @property (nonatomic, strong, readwrite) UIImage *filterImage;
  14. @property (nonatomic, strong, readwrite) UIImage *QRCodeImage;
  15.  
  16. @end
  17.  
  18. @implementation CIFilterEffect
  19.  
  20. - (instancetype)initWithImage:(UIImage *)image filterName:(NSString *)name
  21. {
  22. self = [super init];
  23. if (self)
  24. {
  25. // 将UIImage转换成CIImage
  26. CIImage *ciImage = [[CIImage alloc] initWithImage:image];
  27.  
  28. // 创建滤镜
  29. CIFilter *filter = [CIFilter filterWithName:name
  30. keysAndValues:kCIInputImageKey, ciImage, nil];
  31. [filter setDefaults];
  32.  
  33. // 获取绘制上下文
  34. CIContext *context = [CIContext contextWithOptions:nil];
  35.  
  36. // 渲染并输出CIImage
  37. CIImage *outputImage = [filter outputImage];
  38.  
  39. // 创建CGImage句柄
  40. CGImageRef cgImage = [context createCGImage:outputImage
  41. fromRect:[outputImage extent]];
  42.  
  43. _filterImage = [UIImage imageWithCGImage:cgImage];
  44.  
  45. // 释放CGImage句柄
  46. CGImageRelease(cgImage);
  47. }
  48. return self;
  49. }
  50.  
  51. - (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width
  52. {
  53. self = [super init];
  54. if (self)
  55. {
  56. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  57.  
  58. [filter setDefaults];
  59.  
  60. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  61.  
  62. [filter setValue:data
  63. forKey:@"inputMessage"];
  64.  
  65. CIImage *outputImage = [filter outputImage];
  66.  
  67. CIContext *context = [CIContext contextWithOptions:nil];
  68. CGImageRef cgImage = [context createCGImage:outputImage
  69. fromRect:[outputImage extent]];
  70.  
  71. UIImage *image = [UIImage imageWithCGImage:cgImage
  72. scale:0.1
  73. orientation:UIImageOrientationUp];
  74.  
  75. // 不失真的放大
  76. UIImage *resized = [self resizeImage:image
  77. withQuality:kCGInterpolationNone
  78. rate:5.0];
  79.  
  80. // 缩放到固定的宽度(高度与宽度一致)
  81. _QRCodeImage = [self scaleWithFixedWidth:width image:resized];
  82.  
  83. CGImageRelease(cgImage);
  84. }
  85. return self;
  86. }
  87.  
  88. - (UIImage *)scaleWithFixedWidth:(CGFloat)width image:(UIImage *)image
  89. {
  90. float newHeight = image.size.height * (width / image.size.width);
  91. CGSize size = CGSizeMake(width, newHeight);
  92. UIGraphicsBeginImageContextWithOptions(size, NO, );
  93.  
  94. CGContextRef context = UIGraphicsGetCurrentContext();
  95.  
  96. CGContextTranslateCTM(context, 0.0, size.height);
  97. CGContextScaleCTM(context, 1.0, -1.0);
  98.  
  99. CGContextSetBlendMode(context, kCGBlendModeCopy);
  100. CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
  101.  
  102. UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
  103.  
  104. UIGraphicsEndImageContext();
  105.  
  106. return imageOut;
  107. }
  108.  
  109. - (UIImage *)resizeImage:(UIImage *)image
  110. withQuality:(CGInterpolationQuality)quality
  111. rate:(CGFloat)rate
  112. {
  113. UIImage *resized = nil;
  114. CGFloat width = image.size.width * rate;
  115. CGFloat height = image.size.height * rate;
  116.  
  117. UIGraphicsBeginImageContext(CGSizeMake(width, height));
  118. CGContextRef context = UIGraphicsGetCurrentContext();
  119. CGContextSetInterpolationQuality(context, quality);
  120. [image drawInRect:CGRectMake(, , width, height)];
  121. resized = UIGraphicsGetImageFromCurrentImageContext();
  122. UIGraphicsEndImageContext();
  123.  
  124. return resized;
  125. }
  126.  
  127. @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. Java 数组实现堆栈操作

    class Stack { private int stck[] ; private int tos ; Stack(int size) { // 一个参数的构造参数 stck = new int[s ...

  2. DP Intro - poj 1947 Rebuilding Roads

    算法: dp[i][j]表示以i为根的子树要变成有j个节点的状态需要减掉的边数. 考虑状态转移的时候不考虑i的父亲节点,就当不存在.最后统计最少减去边数的 时候+1. 考虑一个节点时,有两种选择,要么 ...

  3. 前端中用到的图片(png图片)

    作为前端工程师,将设计师的设计稿转化为html页面,其中有一个必不可少的环节就是将psd文件中的一些图片随心所欲的使用,而我们经常用到的就是png图片. 第一部分:基本介绍 首先我们先对比几种图片: ...

  4. Python数据类型(元组)

    文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 元组 Python的元组与列表类似 ...

  5. RESTful简单介绍

    1 什么是restful Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. 资源:互联网所有的事物都 ...

  6. Ubuntu安装Python的mysqlclient

    介绍 本人想在Ubuntu上开发Python程序,使用MySQL数据库. 安装环境: Ubuntu14.04 安装MySQL数据库 具体步骤如下: apt-get update apt-get ins ...

  7. 2.2、js基础---预解析和严格模式

    一.语言特性         1.预解析:js会把变量的声明(仅仅是声明)提到顶部,但是不会突破作用域.                 alert(a);var a= 12; //结果,undefi ...

  8. IdentityServer4授权模式应用场景

    OpenID 和 OAuth 的区别 IdentityServer4,NET Core下的安全框架 客户端模式(Client Credentials) 密码模式(resource owner pass ...

  9. C++ 隐含的this 指针

    c++primer   页数:376-379 备份, 很有嚼头 #include <iostream> #include <string> #include <fstre ...

  10. 在ASPNETCORE中获得所有Action

    在ASPNETCORE中获得所有Action 本文旨在记录自己在aspnetcore工作中需要获取所有Action,在查询了资料后进行了几种方法的记录.后期有发现其它方式再进行追加. 一.通过 反射 ...