QRCode

作者: KGBer | 来源:发表于2016-05-14 17:12 被阅读0次
    
    // 二维码的内容,NSData 格式,编码建议使用 NSIOSLatin1StringEncoding
    NSData *contentData = [string dataUsingEncoding:NSISOLatin1StringEncoding];
    
    // 使用 CIFilter 创建二维码
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator" withInputParameters:@{@"inputMessage" : contentData,  @"inputCorrectionLevel" : @"M"}];
    
    // 输出图片数据,CIImage 格式
    CIImage *image = filter.outputImage;
    
    // 改尺寸
    CGFloat width = CGRectGetWidth(image.extent);
    CGFloat scale = 200 / width;
    image = [image imageByApplyingTransform:CGAffineTransformMakeScale(scale, scale)];
    
    // 图片上下文
    CIContext *context = [CIContext contextWithOptions:nil];
    
    // 输出图片数据,CGImage 格式,如果要将图片保存到本地,则需要创建 CGImage,否则保存失败
    CGImageRef imageRef = [context createCGImage:image fromRect:image.extent];
    
    // 将 CGImage 转为 UIImage
    UIImage *QRCode = [UIImage imageWithCGImage:imageRef];
    
    // 用完要释放内存,ARC 并不管理 C 类型的对象
    CGImageRelease(imageRef);
    
    // 显示
    [self.view addSubview:[[UIImageView alloc] initWithImage:QRCode]];
    

    相关文章

      网友评论

          本文标题:QRCode

          本文链接:https://www.haomeiwen.com/subject/ceamrttx.html