倒入SDK SDK下载地址
链接:/s/1zt-rALA9Z-_ZvOpmX9JscQ 密码:t8vl
ViewController.m
导入头文件
#import "ViewController.h"#import "QRCodeGenerator.h"
创建一个UITextFiled 用于输入文字
创建一个生成二维码的UiButton
创建一个UIImageView
UITextField *field;UIButton *btn;UIImageView *imgVV;
设置field的位置等
//文本框的位置field = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 100, 200, 44)];//提示文字field.placeholder = @"请输入内容";field.borderStyle = UITextBorderStyleRoundedRect;//添加到视图[self.view addSubview:field];
设置按钮的位置等 记得添加一个点击方法
//按钮的位置btn = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 180, 200, 44)];//为按钮设置文字[btn setTitle:@"点击生成二维码" forState:UIControlStateNormal];//为按钮设置背景颜色btn.backgroundColor = [UIColor lightGrayColor];//给按钮添加一个点击方法[btn addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];
按钮的点击方法内:
初始化ImageView 设置位置 设置二维码 , 二维码内容和生成 以及 把ImageView添加到视图 (最好不要设置ImageView的背景颜色 , 如果设置了背景颜色 文本框内没有内容的情况下 点击生成二维码按钮 , 就会出现一个ImageView纯颜色 )
设置UIImageView的位置imgVV = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 300, 200, 200)];//为UIImageView设置二维码图片imgVV.image = [QRCodeGenerator qrImageForString:field.text imageSize:imgVV.bounds.size.width];//把图片添加到视图[self.view addSubview:imgVV];