1 调用的地方
//生成带边框的圆角图片,这个圆角图标可以先生成,如果放在二维码生成时会影响图片生成速度。
self.logo= [self createNewlogoViewView:centerLogo radius:50 borderColor:[UIColor whiteColor] width:100];
//二维码
NSString *url = [NSStringstringWithFormat:@"%ld",[UserSessionsharedInstance].userId];
if (url.length >0) {
//这是我们项目中的需求,这是二维码字符串的前缀为了UI显示的更好看在扫到之后会根据字符串分割
//这串二维码后缀长度和中间头像的大小会影响到识别结果。如果后缀长度短,并且头像大,就会扫不出来。可以适当调整。
NSString *suff =@"66666666668888888888888";
url = [NSString stringWithFormat:@"%@,%@",url,suff];
self.theCodeImgView.image = [QRCodeCreatercreateQRCodeWithBaBaXiaoMoXianUrlString:urlcenterLogo:self.logo];
}
vc.block_scan = ^(NSString *result) {
if ([result containsString:@","]) {
NSArray *arr = [result componentsSeparatedByString:@","];
if (arr.count >1) {
//扫描的结果
NSInteger userId = [arr[0]integerValue];
}
}
};
2 生成的代码
typedef NS_OPTIONS(NSUInteger, QRCodeLogoType) {
QRCodeLogoType_Default = 0,//默认无圆角logo
QRCodeLogoType_Round = 1,//正圆logo
QRCodeLogoType_Radius = 2 //圆角的logo
};
/**
生成 URL 对应的二维码图片:带logo,有三种类型logo,1不带圆角,2正圆,3圆角。
不支持设置logo图片大小,不支持设置自定义圆角大小。
@param url 二维码URL
@param centerLogo 图片
@param logoType logo样式:
*/
+ (UIImage *)createQRCodeImageWithString:(NSString *)url centerLogo:(UIImage *)centerLogo logoType:(QRCodeLogoType)logoType;
//生成的图片
+ (UIImage *)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets layer:(CALayer *)theLayer{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
[theLayer renderInContext:currentContext];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
NSData * data = UIImageJPEGRepresentation(snapshotImage, 1);
UIGraphicsEndImageContext();
UIImage *img = [UIImage imageWithData:data];
return img;
}
//生成带圆角的logo,logo不带边框。
+ (UIImage *)createQRCodeImageWithString:(NSString *)url centerLogo:(UIImage *)centerLogo logoType:(QRCodeLogoType)logoType{
NSArray *filter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
// NSLog(@"%@", filter);
// 二维码过滤器
CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 将二位码过滤器设置为默认属性
[filterImage setDefaults];
// 将文字转化为二进制
NSData *dataImage = [url dataUsingEncoding:NSUTF8StringEncoding];
// 打印输入的属性
// NSLog(@"%@", filterImage.inputKeys);
// KVC 赋值
[filterImage setValue:dataImage forKey:@"inputMessage"];
// 取出输出图片
CIImage *outputImage = [filterImage outputImage];
outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
// 转化图片
UIImage *image = [UIImage imageWithCIImage:outputImage];
//生成图片,并获取上下文,得到一个带头像的二维码图片
image = [self createNewImage:image logoImage:centerLogo logoType:logoType];
return image;
}
+ (UIImage *)createNewImage:(UIImage *)theImage logoImage:(UIImage *)logoImage logoType:(QRCodeLogoType)logoType{
float width = 400;
float height = width * (theImage.size.height / theImage.size.width);
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width,height)];
view.backgroundColor = [UIColor whiteColor];
UIImageView *bacImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width,height)];
bacImageView.image = theImage;
bacImageView.backgroundColor = [UIColor whiteColor];
[view addSubview:bacImageView];
//logoImage.size.width;
float width_logo = width / 5;
UIView *logoBacView = [[UIView alloc]initWithFrame:CGRectMake((width - width_logo)/2 , (height - width_logo)/2, width_logo + 10,width_logo + 10)];
logoBacView.backgroundColor = [UIColor whiteColor];
logoBacView.layer.masksToBounds = YES;
[bacImageView addSubview:logoBacView];
UIImageView *centerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(5,5, width_logo,width_logo)];
centerImgView.image = logoImage;
centerImgView.layer.masksToBounds = YES;
centerImgView.backgroundColor = [UIColor whiteColor];
[logoBacView addSubview:centerImgView];
//设置圆角
float radius = 0;
float radius_img = 0;
if (logoType == QRCodeLogoType_Round) {
radius = (width_logo + 10)/2;
radius_img = width_logo/2;
}else if (logoType == QRCodeLogoType_Radius){
radius = 15;
radius_img = 15;
}else {
radius = 0;
radius_img = 0;
}
logoBacView.layer.cornerRadius = radius;
centerImgView.layer.cornerRadius = radius_img;
//生成图片
UIImage *img = [self snapshotViewFromRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) withCapInsets:UIEdgeInsetsZero layer:view.layer];
return img;
}