二维码

生成二维码

前提:需要导入以下代码

 

1.在ViewController的.m文件中定义一个UIImageView属性, 存放二维码

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

2.在viewDidLoad方法中写以下代码即可

self.imageView.image = [QRCodeGenerator qrImageForString:@"欢欢"imageSize:self.imageView.frame.size.width];

 

二维码扫描:在真机上才可以

前提:需要引入系统框架

 

#import <AVFoundation/AVFoundation.h>

1.定义属性

@property(nonatomic, strong)AVCaptureSession *captureSession;

2.在viewDidLoad中写以下代码

self.captureSession = [[AVCaptureSession alloc] init];

    //自定义相机界面

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    //设置session的输入流

    [_captureSession addInput:input];

    //设置session的输出流

    AVCaptureMetadataOutput *outPut = [[AVCaptureMetadataOutput alloc] init];

    //为输出流设置代理

    [outPut setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    //设置扫码类型

    [_captureSession addOutput:outPut];

    //二维码和人脸

    [outPut setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeFace]];

    //生成相机涂层

    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];

    layer.frame = self.view.bounds;

    [self.view.layer addSublayer:layer];

    //开始扫描

    [self.captureSession startRunning];

 

posted @ 2016-01-25 22:12  星空梦之蓝  阅读(332)  评论(0编辑  收藏  举报