首先看二维码生成
首先引入框架
#import <CoreImage/CoreImage.h>
代码
// 1.实例化二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filter setDefaults];
![](/qrcode.jpg)
// 3.将字符串转换成NSdata
NSData *data = [@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];
// 5.生成二维码
CIImage *outputImage = [filter outputImage];
// UIImage *image = [UIImage imageWithCIImage:outputImage]; 这种生成的图片会模糊 下面对图片进行处理
//处理后代码为这样 得到最后的图片
UIImage *image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
self.codeImageV.image = finalImg;
// 对图片进行处理
- (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,代码如下 下面的代码跟上面没关系 是独立的 只是下面这个中间加了个logo
// 1.实例化二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性)
[filter setDefaults];
// 3.将字符串转换成NSdata
NSData *data = [@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码
[filter setValue:data forKey:@"inputMessage"];
// 5.生成二维码
CIImage *outputImage = [filter outputImage];
CGAffineTransform scale=CGAffineTransformMakeScale(10, 10);
outputImage=[outputImage imageByApplyingTransform:scale];
//6.在中心增加一张图片
UIImage *img=[UIImage imageWithCIImage:outputImage];
//7.1开启图形上下文
UIGraphicsBeginImageContext(img.size);
//7.2将二维码的图片画入
[img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
//7.3在中心划入其他图片
UIImage *centerImg=[UIImage imageNamed:@"logo"];
CGFloat centerW=70;
CGFloat centerH=70;
CGFloat centerX=(img.size.width-70)*0.5;
CGFloat centerY=(img.size.height -70)*0.5;
[centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];
//7.4获取绘制好的图片
UIImage *finalImg=UIGraphicsGetImageFromCurrentImageContext();
//7.5关闭图像上下文
UIGraphicsEndImageContext();
// UIImage *image = [UIImage imageWithCIImage:outputImage];
// UIImage *image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
self.codeImageV = [UIImageView new];
[self.view addSubview:_codeImageV];
[_codeImageV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view.mas_centerX);
make.centerY.mas_equalTo(self.view.mas_centerY).offset(-50);
make.width.height.mas_equalTo(200);
}];
self.codeImageV.image = finalImg;