- (UIImage *)createQRCodeWithTargetString:(NSString *)targetString logoImage:(UIImage *)logoImage {
// 1.创建一个二维码滤镜实例
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
// 2.给滤镜添加数据
NSString *targetStr = targetString;
NSData *targetData = [targetStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
[filter setValue:targetData forKey:@"inputMessage"];
// 3.生成二维码
CIImage *image = [filter outputImage];
// 4.高清处理: size 要大于等于视图显示的尺寸
UIImage *img = [self createNonInterpolatedUIImageFromCIImage:image size:[UIScreen mainScreen].bounds.size.width];
//5.嵌入LOGO
//5.1开启图形上下文
UIGraphicsBeginImageContext(img.size);
//5.2将二维码的LOGO画入
[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
UIImage *centerImg = logoImage;
CGFloat centerW=img.size.width*0.25;
CGFloat centerH=centerW;
CGFloat centerX=(img.size.width-centerW)*0.5;
CGFloat centerY=(img.size.height -centerH)*0.5;
[centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
//5.3获取绘制好的图片
UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
//5.4关闭图像上下文
UIGraphicsEndImageContext();
//6.生成最终二维码
return finalImg;
}
- (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image size:(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];
}
iOS-生成二维码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...