• iOS开发——生成二维码——工具类


          啥也不说,直接上源码,拷过去就能用。生成二维码的工具类使用方法在ProduceQRCode.h里有示例说明

       分别将下面的ProduceQRCode.h和ProduceQRCode.m对应的代码考到自己创建.h和.m文件中即可。

    ProduceQRCode.h文件源码

    //
    //  ProduceQRCode.h
    //  iOS生成二维码图片
    //
    //  Created by 刘成利 on 15/10/13.
    //  Copyright © 2015年 LiuChengLi. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    /**
     *     二维码的中间添加小图片或logo:
     *  
          由于生成的二维码被扫描时有一定的容错能力,成利记得是容错30%。
          所以二维码中间带有小图片或logo的方法就是在已经生成好的二维码中间的上面再添加一张小的方形图片。
          后期美工可以自己添加或合成上去。
     *
     */
    
    /**
     *  生成二维码图片工具类的使用--示例:
     
     - (void)viewDidLoad {
        [super viewDidLoad];
     
        // 需要转换成二维码字符串
        NSString *showCode = @"http://blog.csdn.net/it_liuchengli";
     
        // 转换后的二维码图片
        UIImage *img = [[ProduceQRCode alloc]initWithQRCodeString:showCode 200].QRCodeImage;
     
     
        // 显示上面转换后的二维码图片
     
        UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
        imgView.center = self.view.center;
        [self.view addSubview:imgView];
    
     }
     */
    
    @interface ProduceQRCode : NSObject
    
    // 生成的二维码图片在这里
    @property (nonatomic, strong, readonly) UIImage *QRCodeImage;
    
    // 直接初始化调用此类alloc后的init如下的方法名
    - (instancetype)initWithQRCodeString:(NSString *)string (CGFloat)width;
    @end

    ProduceQRCode.m文件中的代码

    //
    //  ProduceQRCode.m
    //  iOS生成二维码图片
    //
    //  Created by 刘成利 on 15/10/13.
    //  Copyright © 2015年 LiuChengLi. All rights reserved.
    //
    
    #import "ProduceQRCode.h"
    
    @interface ProduceQRCode ()
    
    
    @end
    
    @implementation ProduceQRCode
    
    
    
    - (instancetype)initWithQRCodeString:(NSString *)string (CGFloat)width{
        
        self = [super init];
        
        
        if (self)
        {
            CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
            
            [filter setDefaults];
            
            NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
            
            [filter setValue:data
                      forKey:@"inputMessage"];
            
            CIImage *outputImage = [filter outputImage];
            
            CIContext *context = [CIContext contextWithOptions:nil];
            CGImageRef cgImage = [context createCGImage:outputImage
                                               fromRect:[outputImage extent]];
            
            UIImage *image = [UIImage imageWithCGImage:cgImage
                                                 scale:0.1
                                           orientation:UIImageOrientationUp];
            
            // 不失真的放大
            UIImage *resized = [self resizeImage:image
                                     withQuality:kCGInterpolationNone
                                            rate:5.0];
            
            // 缩放到固定的宽度(高度与宽度一致)
            _QRCodeImage = [self scaleWithFixedWidth:width image:resized];
            
            CGImageRelease(cgImage);
        }
        return self;
    
    }
    
    - (UIImage *)scaleWithFixedWidth:(CGFloat)width image:(UIImage *)image
    {
        float newHeight = image.size.height * (width / image.size.width);
        CGSize size = CGSizeMake(width, newHeight);
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextTranslateCTM(context, 0.0, size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
        
        UIImage *imageOut = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return imageOut;
    }
    
    - (UIImage *)resizeImage:(UIImage *)image
                 withQuality:(CGInterpolationQuality)quality
                        rate:(CGFloat)rate
    {
        UIImage *resized = nil;
        CGFloat width = image.size.width * rate;
        CGFloat height = image.size.height * rate;
        
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, quality);
        [image drawInRect:CGRectMake(0, 0, width, height)];
        resized = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return resized;
    }
    
    
    @end



  • 相关阅读:
    Java方向如何准备技术面试答案(汇总版)
    HTTP与HTTPS的区别
    MyBatisPlus环境下使用MyBatis的配置类
    idea 插件的使用 进阶篇(个人收集使用中的)
    Leveldb实现原理
    浅析 Bigtable 和 LevelDB 的实现
    IKAnalyzer进行中文分词和去停用词
    Elasticsearch之中文分词器插件es-ik的自定义词库
    使用Java High Level REST Client操作elasticsearch
    任正非:坚持逐渐辞退低绩效员工
  • 原文地址:https://www.cnblogs.com/LiuChengLi/p/4875236.html
Copyright © 2020-2023  润新知