QRCodeImageObj.h
@interface QRCodeImageObj : NSObject
///文字生成二维码图片
- (UIImage *)extCreateQRCodeWithUrl:(NSString *)url;
/// 添加logo, 将newImage添加到sourceImage,位置居中
- (UIImage *)extAddLogoImage:(UIImage *)newImage onImage:(UIImage *)sourceImage;
///保存到相册
- (void)extSave2AlbumWithImage:(UIImage *)image;
@end
QRCodeImageObj.m
#import "QRCodeImageObj.h"
#import <CoreImage/CoreImage.h>
@implementation QRCodeImageObj
///文字生成二维码图片
- (UIImage *)extCreateQRCodeWithUrl:(NSString *)url {
// 1. 创建一个二维码滤镜实例(CIFilter)
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filter setDefaults];
// 2. 给滤镜添加数据
NSString *string = url;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// 使用KVC的方式给filter赋值
[filter setValue:data forKeyPath:@"inputMessage"];
// 3. 生成二维码
CIImage *image = [filter outputImage];
// 转成高清格式
UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:image withSize:200];
// 添加logo
qrcode = [self extAddLogoImage:[UIImage imageNamed:@"icon"] onImage:qrcode];
return qrcode;
}
/// 将二维码转成高清的格式
- (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];
}
/// 添加logo, 将newImage添加到sourceImage,位置居中
- (UIImage *)extAddLogoImage:(UIImage *)newImage onImage:(UIImage *)sourceImage {
//画的背景 大小
CGSize imageSize = [sourceImage size];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
//获得 图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//画 自己想要画的内容(添加的图片)
CGContextDrawPath(context, kCGPathStroke);
// 注意logo的尺寸不要太大,否则可能无法识别
CGRect rect = CGRectMake(imageSize.width / 2 - 25, imageSize.height / 2 - 25, 50, 50);
// CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[newImage drawInRect:rect];
//返回绘制的新图形
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSLog(@"%@ err:%@", NSStringFromSelector(_cmd), error.description);
}
///保存到相册
- (void)extSave2AlbumWithImage:(UIImage *)image {
if (nil == image) {
return;
}
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSLog(@"%@ err:%@", NSStringFromSelector(_cmd), error.description);
}
@end
demo
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
QRCodeImageObj *obj = [[QRCodeImageObj alloc] init];
self.ivCode.image = [obj extCreateQRCodeWithUrl:@"abc"];
}
网友评论