• 二维码生成


    #import "ViewController.h"

    #import <CoreImage/CoreImage.h>

     

    @interface ViewController ()

     

    @property (weak, nonatomic) IBOutlet UIImageView *imageView;

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        // 1.创建滤镜对象

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

        

        // 2.设置相关属性

        [fiter setDefaults];

        

        // 3.设置输入数据

        NSString *inputData = @"http://www.baidu.com";

        NSData *data = [inputData dataUsingEncoding:NSUTF8StringEncoding];

        [fiter setValue:data forKeyPath:@"inputMessage"];

        

        // 4.获取输出结果

        CIImage *outputImage = [fiter outputImage];

        

        // 5.显示二维码

        self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

    }

     

    /**

     *  根据CIImage生成指定大小的UIImage

     *

     *  @param image CIImage

     *  @param size  图片宽度

     */

    - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size

    {

        CGRect extent = CGRectIntegral(image.extent);

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

        

        // 1.创建bitmap;

        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);

        

        // 2.保存bitmap到图片

        CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

        CGContextRelease(bitmapRef);

        CGImageRelease(bitmapImage);

        return [UIImage imageWithCGImage:scaledImage];

    }

     

    @end

  • 相关阅读:
    docker在Linux环境下的安装
    docker在Windows环境下的安装
    tcpdump和windump
    Centos7下安装Elasticsearch 5.6.6
    使用concurrent.futures模块并发,实现进程池、线程池
    Nginx配置Gzip
    linux常用命令
    Linux下文档与目录结构
    快速读取大文件的几种方式
    linux 将大文件分解为多个小文件
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5484234.html
Copyright © 2020-2023  润新知