.h
//根据字符串生成指定大小的二维码 + (UIImage *)createQRCodeImageWithMessage:(NSString *)message size:(CGFloat)size;
.m
#import <CoreImage/CoreImage.h>//用于生成二维码 //根据字符串生成指定大小的二维码 + (UIImage *)createQRCodeImageWithMessage:(NSString *)message size:(CGFloat)size { // 1.创建过滤器,这里的@"CIQRCodeGenerator"是固定的 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2.恢复默认设置 [filter setDefaults]; // 3. 给过滤器添加数据 NSString *dataString = message; NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding]; // 注意,这里的value必须是NSData类型 [filter setValue:data forKeyPath:@"inputMessage"]; // 4. 生成二维码 CIImage *outputImage = [filter outputImage]; // 5. 输出二维码 return [self creatNonInterpolatedUIImageFormCIImage:outputImage withSize:size]; } /** 根据CIImage生成指定大小的UIImage @param image CIImage @param size 图片宽度 @return 生成的高清的UIImage */ + (UIImage *)creatNonInterpolatedUIImageFormCIImage:(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]; }