一、生成二维码
1、生成过滤器
CIFilter*filer = [CIFilterfilterWithName:@"CIQRCodeGenerator"];
2、恢复默认设置
[filersetDefaults];
3、向二维码中添加数据
NSString*string = [NSStringstringWithFormat:@"傻屌"];
NSData*data = [stringdataUsingEncoding:NSUTF8StringEncoding];
[filersetValue:dataforKeyPath:@"inputMessage"];
4、从过滤器中获取到二维码
CIImage*ciImage = [fileroutputImage];
5、把获取到的二维码数据加载到视图中
self.CodeImageView.image= [UIImageimageWithCIImage:ciImage];
二、扫描二维码
// 1、创建捕捉会话
AVCaptureSession*session = [[AVCaptureSessionalloc]init];
// 2、设置捕捉的输入设备
AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput*inPutDevice = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];
[sessionaddInput:inPutDevice];
// 3、设置捕捉的输出数据
AVCaptureMetadataOutput*MetadataOutput = [[AVCaptureMetadataOutputalloc]init];
[MetadataOutputsetMetadataObjectsDelegate:selfqueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];
[sessionaddOutput:MetadataOutput];
[MetadataOutputsetMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4、添加捕捉图层
AVCaptureVideoPreviewLayer*preViewLayer = [AVCaptureVideoPreviewLayerlayerWithSession:session];
preViewLayer.frame=self.view.bounds;
[self.view.layeraddSublayer:preViewLayer];
// 5、执行捕捉
[sessionstartRunning];