1.iOS7以后,开始集成了二维码的生成和读取功能
2.生成二维码的步骤:
导入CoreImage框架
通过滤镜CIFilter生成二维码
简单的代码案例:
//创建过滤器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//设置默认
[filter setDefaults];
//给过滤器添加数据
NSString *dataString = @"链接......";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
//3).通过KVO设置滤镜inputMessage数据
[filter setValue:data forKey:@"inputMessage"];
//4).获取输出的二维码
CIImage *outputImage = [filter outputImage];
//5).将CIImage转化成UImage ,并放大显示
self.iconImgView.image = [UIImage imageWithCIImage:outputImage scale:20.0 orientation:UIImageOrientationUp];
3.将CIImage转化成UImage ,并放大显示
// 创建放大的系数
CGAffineTransform tranform = CGAffineTransformMakeScale(10, 10);
// 根据放大系数放大的图片
CIImage *scaleImage = [slCIImage imageByApplyingTransform:tranform];
UIImage *image = [UIImage imageWithCIImage:scaleImage];
4.将头像画到上下文中
//开启上下文
UIImage *drawImg = img;
UIGraphicsBeginImageContext(img.size);
//将图片画到上下文center
[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//获取头像,将图片画到上下文中
UIImage *iconImg = [UIImage imageNamed:iconString];
if (iconImg != nil) {
CGFloat width = 60;
CGFloat height = 60;
CGFloat originX ;
CGFloat originY ;
if (iconSize.width != 0 && iconSize.height != 0) {
width = iconSize.width;
height = iconSize.height;
}
originX = (img.size.width - width) * 0.5;
originY = (img.size.height - height) * 0.5;
[iconImg drawInRect:CGRectMake(originX, originY, width, height)];
//从上下文获取合成的新图片
drawImg = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext(); //关闭上下文