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

  • 相关阅读:
    HDU1285-确定比赛名次(拓扑排序)
    ftp sftp
    Python with 用法
    odoo 非root用户运行不成功
    linux 删除软连接
    vscode wsl php
    WSL 修改默认登录用户为root
    WSL ssh服务自启动
    odoo 获取model的所有字段
    odoo 在"动作"("Action")菜单中添加子菜单, 点击子菜单弹窗自定义form
  • 原文地址:https://www.cnblogs.com/supersr/p/4969510.html
Copyright © 2011-2022 走看看