标签:
二维码生成的具体代码
1 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 2 3 //创建过滤器 4 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; 5 6 //过滤器恢复默认 7 [filter setDefaults]; 8 9 //给过滤器添加数据 10 NSString *string = @"http://www.cnblogs.com/PSSSCode/"; 11 12 //将NSString格式转化成NSData格式 13 NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 14 15 [filter setValue:data forKeyPath:@"inputMessage"]; 16 17 //获取二维码过滤器生成的二维码 18 CIImage *image = [filter outputImage]; 19 20 //将获取到的二维码添加到imageview上 21 self.imageView.image =[UIImage imageWithCIImage:image];
从上图可以看出生成的二维码模糊不清晰,在上面代码块最后一步换成调用下面代码块中的方法即可
1 /** 2 * 根据CIImage生成指定大小的UIImage 3 * 4 * @param image CIImage 5 * @param size 图片宽度 6 */ 7 - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size 8 { 9 CGRect extent = CGRectIntegral(image.extent); 10 CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); 11 12 // 1.创建bitmap; 13 size_t width = CGRectGetWidth(extent) * scale; 14 size_t height = CGRectGetHeight(extent) * scale; 15 CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); 16 CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); 17 CIContext *context = [CIContext contextWithOptions:nil]; 18 CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; 19 CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); 20 CGContextScaleCTM(bitmapRef, scale, scale); 21 CGContextDrawImage(bitmapRef, extent, bitmapImage); 22 23 // 2.保存bitmap到图片 24 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); 25 CGContextRelease(bitmapRef); 26 CGImageRelease(bitmapImage); 27 return [UIImage imageWithCGImage:scaledImage]; 28 }
正常代码生成的二维码 调用代码后生成的二维码
标签:
原文地址:http://www.cnblogs.com/PSSSCode/p/5487390.html