画带图片的高清二维码
-
(void)createQR{
// 1. 创建一个二维码滤镜实例(CIFilter)
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filter setDefaults];
// 2. 给滤镜添加数据
NSString *string = [NSString stringWithFormat:@"http://www.lvlvchat.com/mobile/lvchatregister?from=%@",self.ID];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// 使用KVC的方式给filter赋值
[filter setValue:data forKeyPath:@"inputMessage"];// 3. 生成二维码
CIImage *image = [filter outputImage];
// _qrImg = [UIImage imageWithCIImage:image];
// 4. 显示二维码
_qrImg = [self createNonInterpolatedUIImageFormCIImage:image withSize:200];
//画图
UIGraphicsBeginImageContextWithOptions(_qrImg.size,NO,0.0);
[_qrImg drawInRect:CGRectMake(0, 0, _qrImg.size.width, _qrImg.size.height)];
UIImage *smallImage = [UIImage imageNamed:@"centerImg"];
[smallImage drawInRect:CGRectMake((_qrImg.size.width-KRate(50))/2, (_qrImg.size.width - KRate(50))/2, KRate(50), KRate(50))];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_qrImg = finalImage;
self.imageView.image = _qrImg;
}
-
(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);UIImage *img = [UIImage imageWithCGImage:scaledImage];
return img;
}