很简单,就是几句代码。
// 创建一个uiimageView装生成的二维码图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, 100, 100)];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
// 生成二维码图片
NSString *url = @"http://www.jianshu.com/u/40ab6b5c782a";
CIImage *barcodeImage;
NSData *data = [url dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];// 得到二维码图片
[filter setValue:@"H" forKey:@"inputCorrectionLevel"];//设置二维码的纠错水平,越高纠错水平越高,可以污损的范围越大
barcodeImage = [filter outputImage];
// 消除模糊
CGFloat scaleX = imageView.frame.size.width / barcodeImage.extent.size.width; // extent 返回图片的frame
CGFloat scaleY = imageView.frame.size.height / barcodeImage.extent.size.height;
CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
imageView.image = [UIImage imageWithCIImage:transformedImage];
生成条形码就是将里面的@"CIQRCodeGenerator"换成@"CICode128BarcodeGenerator"并且将 [filter setValue:@"H" forKey:@"inputCorrectionLevel"];注释掉,其他的都是一样的。