根据链接生成二维码

http://www.cnblogs.com/gchlcc/p/5585482.html

导入

-(void)creatQRcode:(NSString *)url

{

// 1.创建过滤器 -- 苹果没有将这个字符封装成常量

CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

// 2.过滤器恢复默认设置

[filter setDefaults];

// 3.给过滤器添加数据(正则表达式/帐号和密码) -- 通过KVC设置过滤器,只能设置NSData类型

NSData *data = [url dataUsingEncoding:NSUTF8StringEncoding];

[filter setValue:data forKeyPath:@"inputMessage"];

// 4.获取输出的二维码

CIImage *outputImage = [filter outputImage];

//    self.tgImageView.image=[UIImage imageWithCIImage:outputImage];

UIImage *image=[self createNonInterpolatedUIImageFormCIImage:outputImage withSize:100];

self.tgImageView.image=image;

}

//处理二维码

- (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];

}

你可能感兴趣的:(根据链接生成二维码)