生成二維碼:
1. 下載libqrencode三方後, 拉入工程
2. 在頭文件引用 #import “QRCodeGenerator.h”
3. 效果圖:
4.
代碼:
// 文本框
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 200, 50)];
[self.view addSubview:textfield];
textfield.backgroundColor = [UIColor grayColor];
textfield.placeholder = @"請輸入文字";
textfield.tag = 1;
// 按鈕
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(250, 20, 50, 50);
[self.view addSubview:button];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"生成" forState:UIControlStateNormal];
[button addTarget:self action:@selector(QRCode) forControlEvents:UIControlEventTouchUpInside];
方法:
- (void)QRCode {
UITextField *textfield = [self.view viewWithTag:1];
UIImage *image = [QRCodeGenerator qrImageForString:textfield.text imageSize:200];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 70, 200, 200)];
imageView.center = self.view.center;
imageView.image = image;
[self.view addSubview:imageView];
}
warning
如果在編譯中爆出c99錯誤, 可以在#import “QRCodeGenerator.h”文件中, 引用: