二维码<来自网络感谢iOs世界交流群的提供>

二维码<来自网络感谢iOs世界交流群的提供>_第1张图片
关键生成二维码的过程
二维码<来自网络感谢iOs世界交流群的提供>_第2张图片
.h文件


二维码<来自网络感谢iOs世界交流群的提供>_第3张图片
点击按钮和输入框的内容


----------------------代码---------.h

#import

@interfacezyViewController :UIViewController

@property(nonatomic,strong)UITextField*tfCode;

@property(nonatomic,strong)UIButton*btnGenerate;

@property(nonatomic,strong)UIImageView*imageView;

@end

--------------------------------.m

#import"zyViewController.h"

@interfacezyViewController()

@end

@implementationzyViewController

- (void)viewDidLoad {

[superviewDidLoad];

CGSizewindowSize = [UIScreenmain Screen].bounds.size;

self.tfCode= [[UITextField  alloc]initWithFrame:CGRectMake(10, 64, windowSize.width-100, 40)];

[self.view    addSubview:self.tfCode];

self.tfCode.borderStyle=UITextBorderStyleRoundedRect;

self.btnGenerate= [[UIButtonalloc]initWithFrame:CGRectMake(windowSize.width-100, 64, 90, 40)];

[self.viewaddSubview:self.btnGenerate];

[self.btnGenerateaddTarget:selfaction:@selector(actionGenerate)forControlEvents:UIControlEventTouchUpInside];

self.btnGenerate.backgroundColor= [UIColorlightGrayColor];

[self.btnGeneratesetTitle:@"生成"forState:UIControlStateNormal];

[self.btnGeneratesetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];

self.imageView= [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 300, 300)];

[self.viewaddSubview:self.imageView];

self.imageView.center=CGPointMake(windowSize.width/2, windowSize.height/2);

self.tfCode.text=@"http://www.baidu.com";

}

- (void)actionGenerate

{

NSString*text =self.tfCode.text;

NSData*stringData = [textdataUsingEncoding:NSUTF8StringEncoding];

//生成 CIFilter 相当于保存数据的字典

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

[qrFilter   setValue:stringDataforKey:@"inputMessage"];

[qrFilter  setValue:@"M"forKey:@"inputCorrectionLevel"];

UIColor*onColor = [UIColor  redColor];

UIColor*offColor = [UIColor  whiteColor];

//上色

CIFilter*colorFilter = [CIFilter  filterWithName:@"CIFalseColor"keysAndValues:@"inputImage",qrFilter.outputImage,@"inputColor0",[CIColor  colorWithCGColor:onColor.CGColor],@"inputColor1",[CIColor   colorWithCGColor:offColor.CGColor],nil];

CIImage*qrImage = colorFilter.outputImage;

//绘制

CGSizesize =CGSizeMake(300, 300);

CGImageRefcgImage = [[CIContext  contextWithOptions:nil]createCGImage:qrImagefromRect:qrImage.extent];

UIGraphicsBeginImageContext(size);

CGContextRefcontext =UIGraphicsGetCurrentContext();

CGContextSetInterpolationQuality(context,kCGInterpolationNone);

CGContextScaleCTM(context, 1.0, -1.0);

CGContextDrawImage(context,CGContextGetClipBoundingBox(context), cgImage);

UIImage*codeImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGImageRelease(cgImage);

self.imageView.image= codeImage;

}- (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

{

[self.viewendEditing:YES];

}

@end

你可能感兴趣的:(二维码<来自网络感谢iOs世界交流群的提供>)