1.先 #import <CoreImage/CoreImage.h>
2. //本地生成二维码
// 1.创建过滤器 -- 苹果没有将这个字符封装成常量
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.过滤器恢复默认设置
[filter setDefaults];
// 3.给过滤器添加数据(正则表达式/帐号和密码) -- 通过KVC设置过滤器,只能设置NSData类型
NSString *dataString = @"www.baidu.com";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKeyPath:@"inputMessage"];
// 4.获取输出的二维码
CIImage *outputImage = [filter outputImage];
self.CodeImageView.image = [UIImage imageWithCIImage:outputImage];
self.QRCodeImageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
3、
/重绘输出的二维码名片,使其更清晰
- (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];
}
来源:https://www.cnblogs.com/-ios/p/9592614.html