啥也不说,直接上源码,拷过去就能用。生成二维码的工具类使用方法在ProduceQRCode.h里有示例说明
分别将下面的ProduceQRCode.h和ProduceQRCode.m对应的代码考到自己创建.h和.m文件中即可。
ProduceQRCode.h文件源码
<span style="color:#000000;"><span style="color:#FF0000;">//
// ProduceQRCode.h
// iOS生成二维码图片
//
// Created by 刘成利 on 15/10/13.
// Copyright © 2015年 LiuChengLi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/**
* 二维码的中间添加小图片或logo:
*
由于生成的二维码被扫描时有一定的容错能力,成利记得是容错30%。
所以二维码中间带有小图片或logo的方法就是在已经生成好的二维码中间的上面再添加一张小的方形图片。
后期美工可以自己添加或合成上去。
*
*/
/**
* 生成二维码图片工具类的使用--示例:
- (void)viewDidLoad {
[super viewDidLoad];
// 需要转换成二维码字符串
NSString *showCode = @"http://blog.csdn.net/it_liuchengli";
// 转换后的二维码图片
UIImage *img = [[ProduceQRCode alloc]initWithQRCodeString:showCode width:200].QRCodeImage;
// 显示上面转换后的二维码图片
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
imgView.center = self.view.center;
[self.view addSubview:imgView];
}
*/
@interface ProduceQRCode : NSObject
// 生成的二维码图片在这里
@property (nonatomic, strong, readonly) UIImage *QRCodeImage;
// 直接初始化调用此类alloc后的init如下的方法名
- (instancetype)initWithQRCodeString:(NSString *)string width:(CGFloat)width;
@end</span>
</span>
ProduceQRCode.m文件中的代码
//
// ProduceQRCode.m
// iOS生成二维码图片
//
// Created by 刘成利 on 15/10/13.
// Copyright © 2015年 LiuChengLi. All rights reserved.
//
#import "ProduceQRCode.h"
@interface ProduceQRCode ()
@end
@implementation ProduceQRCode
- (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