第一步:
第二步:
第三步:
注:代码复制区域
- (void)viewDidLoad {
[super viewDidLoad];
NSString *url = @"http://www.baidu.com";
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [self method2:[self method1:url] size:200];
imageView.bounds = CGRectMake(0, 0, 200, 200);
imageView.center = self.view.center;
[self.view addSubview:imageView];
}
//生成二维码,但模糊
- (CIImage *)method1:(NSString *)url {
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults];
NSData *data = [url dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKeyPath:@"inputMessage"];
return [filter outputImage];
}
//模糊处理
- (UIImage *)method2:(CIImage *)image size:(CGFloat)size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
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);
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}