1.首先要导入CoreImage.framework框架
2.引入<CoreImage/CoreImage.h>
头文件
#import <CoreImage/CoreImage.h>
根据文本信息生成二维码
-(UIImage *)generateQRCodeWithInfo:(NSString *)info
{
NSData *data = [info dataUsingEncoding:NSUTF8StringEncoding];
//过滤器
CIFilter *fiter =[CIFilter filterWithName:@"CIQRCodeGenerator"];
//内容和纠错级别
[fiter setValue:data forKey:@"inputMessage"];
[fiter setValue:@"M" forKey:@"inputCorrectionLevel"];
UIImage *image=[UIImage imageWithCIImage:fiter.outputImage];
return image;
}
3.使用方法如下
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
//生成二维码
imv.image=[self generateQRCodeWithInfo:@"http://www.baidu.com"];
[self.view addSubview:imv];
imv.center=self.view.center;
}