iOS开发 - 二维码 ZBarSDK


使用的第三方库 : ZBarSDK


#import "ViewController.h"
//二维码扫描
#import "CustomViewController.h"
//生成二维码
#import "QRCodeGenerator.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //生成二维码所在的image中
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    imageView.center = self.view.center;
    [self.view addSubview:imageView];

    //设置二维码图片
    //第一个参数是 二维码所携带的信息
    //第二个参数是 二维码的清晰度 数值越大 二维码越清晰
    imageView.image = [QRCodeGenerator qrImageForString:@"http://www.baidu.com" imageSize:300];

    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(10, 10, 100, 40);
    [btn setTitle:@"button" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}
-(void)onClick:(UIButton *)btn{

    //二维码扫描
    //第一个参数 YES-表示关闭二维码扫描,只扫描条形码
    CustomViewController *vc = [[CustomViewController alloc] initWithIsQRCode:NO Block:^(NSString * result, BOOL isSuccessed) {
        if (isSuccessed) {
            NSLog(@"扫描结果 %@",result);
        }
    }];
    [self presentViewController:vc animated:YES completion:nil];
}

若自定义扫描二维码的界面 直接修改CustomViewController中的UI

你可能感兴趣的:(iOS开发 - 二维码 ZBarSDK)