第一种:生成普通二维码
方法的调用(这个方法生成普通的二维码):
UIImage *image = [UIImage df_createNonInterpolatedUIImageFormCIImage:[UIImage df_createQRcodeWithUrlstring:@"https://www.baidu.com"] withSize:150]; [self.qrImageView setImage:image];
/**
*生成二维码
*/
+ (CIImage *)df_createQRcodeWithUrlstring:(NSString *)urlString{
// 1.实例化二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filtersetDefaults];
// 3.将字符串转换成NSdata
NSData*data = [urlStringdataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filtersetValue:data forKey:@"inputMessage"];
// 5.生成二维码
CIImage*outputImage = [filteroutputImage];
returnoutputImage;
}
+ (UIImage *)df_createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size{
CGRect extent = CGRectIntegral(image.extent);
CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect: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
方法调用(这个方法可以生成带图片的二维码,建议用第2种方法):
UIImage *image = [UIImage df_drawImage:[UIImage imageNamed:@"XXX"] inImage:[UIImage df_createNonInterpolatedUIImageFormCIImage:[UIImage df_createQRcodeWithUrlstring:@"https://www.baidu.com"] withSize:150]]; [self.qrImageView setImage:image];
+ (UIImage *)df_drawImage:(UIImage *)newImage inImage:(UIImage *)sourceImage;
+ (UIImage *)df_drawImage:(UIImage *)newImage inImage:(UIImage *)sourceImage {
CGSizeimageSize;//画的背景 大小
imageSize = [sourceImagesize];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImagedrawAtPoint:CGPointMake(0,0)];
//获得 图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//画 自己想要画的内容(添加的图片)
CGContextDrawPath(context, kCGPathStroke);
// 注意logo的尺寸不要太大,否则可能无法识别
CGFloatcenterW=sourceImage.size.width*0.25;
CGFloatcenterH=centerW;
CGFloatcenterX=(sourceImage.size.width-centerW)*0.5;
CGFloatcenterY=(sourceImage.size.height-centerH)*0.5;
CGRectrect =CGRectMake(centerX, centerY, centerW, centerH);
// CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[newImagedrawInRect:rect];
//返回绘制的新图形
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
第二种:生成带图片的二维码
方法调用(这个方法可以生成带图片的二维码,建议用这个):
[self.qrImageView setImage:[UIImage df_createInterpolatedUIImageFormCIImageWithUrlstring:@"XXX" withSize:150 iamgeName:@"XXX"]];
+ (UIImage *)df_createInterpolatedUIImageFormCIImageWithUrlstring:(NSString *)urlString withSize:(CGFloat)size iamgeName:(NSString *)imageName{
// 1. 创建一个二维码滤镜实例(CIFilter)
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filtersetDefaults];
// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filtersetDefaults];
// 3.将字符串转换成NSdata
NSData*data = [urlStringdataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filtersetValue:data forKey:@"inputMessage"];
// 5.生成二维码
CIImage*image = [filteroutputImage];
//4.在中心增加一张图片
UIImage *img = [UIImage df_createNonInterpolatedUIImageFormCIImage:image withSize:size];
//5.把中央图片划入二维码里面
//5.1开启图形上下文
UIGraphicsBeginImageContext(img.size);
//5.2将二维码的图片画入
[imgdrawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
UIImage*centerImg = [UIImageimageNamed:imageName];
CGFloatcenterW=img.size.width*0.25;
CGFloatcenterH=centerW;
CGFloatcenterX=(img.size.width-centerW)*0.5;
CGFloatcenterY=(img.size.height-centerH)*0.5;
[centerImgdrawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
//5.3获取绘制好的图片
UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
//5.4关闭图像上下文
UIGraphicsEndImageContext();
return finalImg;
}