zoukankan      html  css  js  c++  java
  • ios创建二维码

    #import "LCTwoCodeImage.h"

     

    @implementation LCTwoCodeImage

     

     

    +(UIImage *) GotoCreatMyTwoCode :(NSString *) string {

        //通过传入的字符串来创建二维码初始图层渲染

        CIImage * firstImage = [self creatCIImageFromString:string];

        //通过 CIImage 来生成二维码

        UIImage * qrCode = [self createNonInterpolatedUIImageFormCIImage:firstImage withSize:250.0f];

        

        return qrCode;

    }

     

     

    +(UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {

        CGRect extent = CGRectIntegral(image.extent);

        CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

        // create a bitmap image that we'll draw into a bitmap context at the desired size;

        size_t width = CGRectGetWidth(extent) * scale;

        size_t height = CGRectGetHeight(extent) * scale;

        CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();

        CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);

        CIContext *context = [CIContext contextWithOptions:nil];

        CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];

        CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);

        CGContextScaleCTM(bitmapRef, scale, scale);

        CGContextDrawImage(bitmapRef, extent, bitmapImage);

        // Create an image with the contents of our bitmap

        CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

        // Cleanup

        CGContextRelease(bitmapRef);

        CGImageRelease(bitmapImage);

        return [UIImage imageWithCGImage:scaledImage];

    }

     

    +(CIImage *)creatCIImageFromString:(NSString *) string {

        NSData * stringData = [string dataUsingEncoding:NSUTF8StringEncoding];

        //添加图层

        CIFilter * qrFiler = [CIFilter filterWithName:@"CIQRCodeGenerator"];

        [qrFiler setValue:stringData forKey:@"inputMessage"];

        [qrFiler setValue:@"M" forKey:@"inputCorrectionLevel"];

        return [qrFiler outputImage];

    }

     

     

     

     

    @end

  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/supersr/p/4969510.html
Copyright © 2011-2022 走看看