博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用CIFilter生成QRCode二维码图片
阅读量:6820 次
发布时间:2019-06-26

本文共 4536 字,大约阅读时间需要 15 分钟。

用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 
/* CILinearToSRGBToneCurveCIPhotoEffectChromeCIPhotoEffectFadeCIPhotoEffectInstantCIPhotoEffectMonoCIPhotoEffectNoirCIPhotoEffectProcessCIPhotoEffectTonalCIPhotoEffectTransferCISRGBToneCurveToLinearCIVignetteEffect */@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, 0);        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(0, 0, width, height)];    resized = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return resized;}@end

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

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

 

 

转载地址:http://kwmzl.baihongyu.com/

你可能感兴趣的文章
ET120以太网环回器介绍
查看>>
ActiveMQ快速入门
查看>>
java自学篇之程序设计基础
查看>>
swiper的基础使用(五)
查看>>
Windows Server 2012R2 Hyper-v之虚拟机复制(2)
查看>>
大数据各种实用网站
查看>>
浅谈NAT概念及原理,配置NAT 网络地址转换---PAT端口地址转换;
查看>>
线上防雪崩利器——熔断器设计原理与实现
查看>>
mongodb 增删查改
查看>>
企业电子邮箱的企业网盘是什么?如何使用?
查看>>
openstack简易汉化
查看>>
centos6.4 ceph安装部署之cephFS
查看>>
“5G”不赶趟?这些国家开始研发“后5G”技术
查看>>
UI设计入门学习方法和学习路线
查看>>
Spark RDD转换成DataFrame的两种方式
查看>>
解决Android第三方库.so自动删除
查看>>
使用RMAN来克隆数据库
查看>>
资源分享redis中文入门手册
查看>>
对开发工具的认识
查看>>
SharePoint中计算值公式函数简介
查看>>