一、.h文件
//
// CIImage+Extension.h
// 根据链接生成生成二维码
//
// Created by zsl on 17/12/25.
// Copyright © 2017年 zsl. All rights reserved.
//
#import
#import
@interfaceCIImage (Extension)
/**
根据CIImage生成指定大小的UIImage
@param size 尺寸
@return 图片对象
*/
- (UIImage*)createNonInterpolatedWithSize:(CGFloat)size;
@end
二、.m文件
//
// CIImage+Extension.m
// 根据链接生成生成二维码
//
// Created by zsl on 17/12/25.
// Copyright © 2017年 zsl. All rights reserved.
//
#import "CIImage+Extension.h"
@implementationCIImage (Extension)
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*/
- (UIImage*)createNonInterpolatedWithSize:(CGFloat)size {
CGRect extent = CGRectIntegral(self.extent);
CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:selffromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
@end
三、引用方式:在调用界面进行引用上面拓展类的头文件#import "CIImage+Extension.h"
// 1.创建滤镜对象
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复默认设置
[filter setDefaults];
//3.链接字符串
NSString *str = @"https://www.jianshu.com/u/4c669da2ffa3";
// 4.将链接字符串转data格式
NSData *strData = [str dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:strData forKeyPath:@"inputMessage"];
// 5.生成二维码
CIImage*outputImage = [filter outputImage];
UIImageView *imageView = [[UIImageView alloc]init];
// 6.将二维码进行展示
imageView = [outputImage createNonInterpolatedWithSize:80];
网友评论