二維碼

生成二維碼:
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”文件中, 引用:

import

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章