用CIFilter生成QRCode二维码图片

简介:

用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, 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

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

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

 

 

目录
相关文章
|
测试技术 调度 微服务
微服务架构下的两类测试
【7月更文挑战第16天】微服务架构下的两类测试:流量录制回放测试和仿真环境测试
|
人工智能 算法 Ubuntu
【朱颜不曾改,芳菲万户香。AIGC人物图片创作---InsCode Stable Diffusion 美图活动一期】
【朱颜不曾改,芳菲万户香。AIGC人物图片创作---InsCode Stable Diffusion 美图活动一期】
1447 2
|
弹性计算 容灾 安全
怎么购买阿里云服务器?
怎么购买阿里云服务器?2023阿里云服务器购买流程更新,选购云服务器有两个入口,一个是选择活动机,只需要选择云服务器地域、系统、带宽即可;另一个是在云服务器页面,自定义选择云服务器配置,这种方式购买云服务器较为复杂,需要选付费方式、地域及可用区、ECS实例规格、镜像、网络、公网IP、安全组等配置,阿里云百科来阿里云服务器购买流程指南2023新版教程:
189 0
怎么购买阿里云服务器?
|
数据采集 SQL JavaScript
实战通过旁注拿下某高权重站群
直接开干! 个人习惯拿到网站后直接nslookup一下,没想到直接解析出来了真实ip
343 0
|
弹性计算 网络协议 网络安全
申请阿里云服务器并远程登录
申请阿里云服务器并远程登录
467 0
申请阿里云服务器并远程登录
【氚云】谈谈在氚云应用中快速实现人员部门精准定位的技巧
谈谈在氚云应用中快速实现人员部门精准定位的技巧
717 0
【氚云】谈谈在氚云应用中快速实现人员部门精准定位的技巧
|
信息无障碍
学习HTML笔记29
HTML 文本格式化
195 0
|
开发者 数据格式
54、拖拉事件
拖拉(drag)指的是,用户在某个对象上按下鼠标键不放,拖动它到另一个位置,然后释放鼠标键,将该对象放在那里。
243 0
|
SQL Oracle Java
oracle执行cmd的实现方法
网络上找到的在sqlplus中执行cmd的一些命令,主要有四种方法,这边都做了一下测试,这里做一下记录: 测试环境:window2003+Oracle 11.2.0.1.0 第一种方法: 最简单的执行cmd命令方法,但执行的本机的cmd命令,非数据库系统的cmd命令,故和本机打开cmd命令一样,貌似没什么用。
1415 0