123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- //
- // CodeHandlerObject.m
- // MIT_Endorsement
- //
- // Created by 翟玉磊 on 2017/10/13.
- // Copyright © 2017年 翟玉磊. All rights reserved.
- //
- #import "CodeHandlerObject.h"
- @implementation CodeHandlerObject
- + (UIImage *)createCodeWithUrl:(NSString *)url withSize:(CGFloat)size {
- // 1. 实例化二维码滤镜
- CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
- // 2. 恢复滤镜的默认属性
- [filter setDefaults];
-
- // 3. 将字符串转换成NSData
- // NSString *urlStr = @"http://www.ychpay.com/down.html";//测试二维码地址,次二维码不能支付,需要配合服务器来二维码的地址(跟后台人员配合)
- NSData *data = [url dataUsingEncoding:NSUTF8StringEncoding];
- // 4. 通过KVO设置滤镜inputMessage数据
- [filter setValue:data forKey:@"inputMessage"];
-
- // 5. 获得滤镜输出的图像
- CIImage *outputImage = [filter outputImage];
-
- // 6. 将CIImage转换成UIImage,并放大显示 (此时获取到的二维码比较模糊,所以需要用下面的createNonInterpolatedUIImageFormCIImage方法重绘二维码)
- // UIImage *codeImage = [UIImage imageWithCIImage:outputImage scale:1.0 orientation:UIImageOrientationUp];
-
- UIImage *codeImage = [CodeHandlerObject createNonInterpolatedUIImageFormCIImage:outputImage withSize:size]; //重绘二维码,使其显示清晰
-
-
- return codeImage;
- }
- /**
- * 根据CIImage生成指定大小的UIImage
- *
- * @param image CIImage
- * @param size 图片宽度
- */
- + (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size
- {
- CGRect extent = CGRectIntegral(image.extent);
- CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
- // 1.创建bitmap;
- size_t width = CGRectGetWidth(extent) * scale;
- size_t height = CGRectGetHeight(extent) * scale;
- CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
- CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
- CIContext *context = [CIContext contextWithOptions:nil];
- CGImageRef bitmapImage = [context createCGImage:image fromRect: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];
- }
- + (UIImage *)codeImageAddLogoWithCodeImage:(UIImage *)codeImage withLogo:(UIImage *)logoImage {
- CGSize size = CGSizeMake(codeImage.size.width,codeImage.size.width);
- CGFloat logoWith = 40;
-
- UIGraphicsBeginImageContextWithOptions(size,NO,[UIScreen mainScreen].scale);//初始化画布
-
- [codeImage drawAtPoint:CGPointZero];//把条形码图片添加到画布中
-
- CGContextRef context = UIGraphicsGetCurrentContext();//画布上下文
-
- CGContextDrawPath(context,kCGPathStroke);
-
- [codeImage drawInRect:CGRectMake(0,0,size.width,size.width)];
-
- [logoImage drawInRect:CGRectMake((size.width - logoWith) / 2,(size.height - logoWith) / 2, logoWith, logoWith)];
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //将画布映射到图片对象
-
- UIGraphicsEndImageContext();//关闭画布
-
- return image;
- }
- //生成的图片
- + (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets layer:(CALayer *)theLayer{
- UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
- CGContextRef currentContext = UIGraphicsGetCurrentContext();
- [theLayer renderInContext:currentContext];
-
- UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return snapshotImage;
- }
- //生成带圆角的logo,logo不带边框。
- + (UIImage *)createQRCodeImageWithString:(NSString *)url centerLogo:(UIImage *)centerLogo logoType:(QRCodeLogoType)logoType withSize:(CGFloat)imageWH {
- // NSLog(@"%@", filter);
- // 二维码过滤器
- CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"];
- // 将二位码过滤器设置为默认属性
- [filterImage setDefaults];
- // 将文字转化为二进制
- NSData *dataImage = [url dataUsingEncoding:NSUTF8StringEncoding];
- // 打印输入的属性
- // NSLog(@"%@", filterImage.inputKeys);
- // KVC 赋值
- [filterImage setValue:dataImage forKey:@"inputMessage"];
- // 取出输出图片
- CIImage *outputImage = [filterImage outputImage];
- outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
- // 转化图片
- UIImage *image = [UIImage imageWithCIImage:outputImage];
-
- if (image == nil) {
- return nil;
- }
- //生成图片,并获取上下文,得到一个带头像的二维码图片
- image = [self createNewImage:image logoImage:centerLogo logoType:logoType withSize:imageWH];
-
- return image;
- }
- + (UIImage *)createNewImage:(UIImage *)theImage logoImage:(UIImage *)logoImage logoType:(QRCodeLogoType)logoType withSize:(CGFloat)imageWH {
- float width = imageWH;
- float height = imageWH;
- UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width,height)];
- view.backgroundColor = [UIColor whiteColor];
-
- UIImageView *bacImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width,height)];
- bacImageView.image = theImage;
- bacImageView.backgroundColor = [UIColor whiteColor];
- [view addSubview:bacImageView];
-
- //logoImage.size.width;
- if (logoImage) {
- float width_logo = width / 5; //logo太大识别不了二维码
- UIView *logoBacView = [[UIView alloc]initWithFrame:CGRectMake((width - width_logo)/2 , (height - width_logo)/2, width_logo + 10,width_logo + 10)];
- logoBacView.backgroundColor = [UIColor whiteColor];
- logoBacView.layer.masksToBounds = YES;
- [bacImageView addSubview:logoBacView];
-
- UIImageView *centerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(5,5, width_logo,width_logo)];
- centerImgView.image = logoImage;
- centerImgView.layer.masksToBounds = YES;
- centerImgView.backgroundColor = [UIColor whiteColor];
- [logoBacView addSubview:centerImgView];
-
- //设置圆角
- float radius = 0;
- float radius_img = 0;
- if (logoType == QRCodeLogoType_Round) {
- radius = (width_logo + 10)/2;
- radius_img = width_logo/2;
- }else if (logoType == QRCodeLogoType_Radius){
- radius = 6;
- radius_img = 6;
- }else {
- radius = 0;
- radius_img = 0;
- }
- logoBacView.layer.cornerRadius = radius;
- centerImgView.layer.cornerRadius = radius_img;
- }
-
-
- //生成图片
- UIImage *img = [self snapshotViewFromRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) withCapInsets:UIEdgeInsetsZero layer:view.layer];
-
- return img;
- }
- /*
- 生成条形码
- @param source
- @return 生成条形码的CIImage对象
- */
- + (CIImage *)generateBarCodeImage:(NSString *)source {
- // iOS 8.0以上的系统才支持条形码的生成,iOS8.0以下使用第三方控件生成
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
- // 注意生成条形码的编码方式
- NSData *data = [source dataUsingEncoding: NSASCIIStringEncoding];
- CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
- [filter setValue:data forKey:@"inputMessage"];
- // 设置生成的条形码的上,下,左,右的margins的值
- [filter setValue:[NSNumber numberWithInteger:0] forKey:@"inputQuietSpace"];
- return filter.outputImage;
- }else{
- return nil;
- }
- }
- /**
-
- 调整生成的图片的大小
- @param image CIImage对象
- @param size 需要的UIImage的大小
- @return size大小的UIImage对象
- */
- + (UIImage *) resizeCodeImage:(CIImage *)image withSize:(CGSize)size {
- if (image) {
- CGRect extent = CGRectIntegral(image.extent);
- CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
- CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
- size_t width = CGRectGetWidth(extent) * scaleWidth;
- size_t height = CGRectGetHeight(extent) * scaleHeight;
- CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
- CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
- CIContext *context = [CIContext contextWithOptions:nil];
- CGImageRef imageRef = [context createCGImage:image fromRect:extent];
- CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
- CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
- CGContextDrawImage(contentRef, extent, imageRef);
- CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
- CGContextRelease(contentRef);
- CGImageRelease(imageRef);
- return [UIImage imageWithCGImage:imageRefResized];
- }else{
- return nil;
- }
- }
- @end
|