iOS 二维码生成

  • 带logo变色的二维码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    //绘图
    - (UIImage *)setCoderWithString:(NSString *)string {
    // 1.创建二维码过滤器
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.设置默认值
    [qrFilter setDefaults];
    /*
    inputMessage, 二维码的内容
    inputCorrectionLevel 二维码的容错率
    */
    NSLog(@"%@",qrFilter.inputKeys);
    // 3.给二维码过滤器添加信息 KVC
    // inputMessage必须要传入二进制 否则会崩溃
    [qrFilter setValue:[string dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
    // 4.获取二维码的图片
    CIImage *image = qrFilter.outputImage;

    image = [image imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
    // 5.创建颜色过滤器
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"];
    // 6.设置默认值
    [colorFilter setDefaults];
    /*
    inputImage, 需要设定颜色的图片
    inputColor0, 前景色 - 二维码的颜色
    inputColor1 背景色 - 二维码背景的颜色
    */
    NSLog(@"%@",colorFilter.inputKeys);
    // 7.给颜色过滤器添加信息
    // 设定图片
    [colorFilter setValue:image forKey:@"inputImage"];
    // 设定前景色
    [colorFilter setValue:[CIColor colorWithRed:(255)/255.0 green:(102)/255.0 blue:(0)/255.0] forKey:@"inputColor0"];
    // 设定背景色
    [colorFilter setValue:[CIColor colorWithRed:1 green:1 blue:1 alpha:0.5] forKey:@"inputColor1"];
    // 获取图片
    image = colorFilter.outputImage;
    UIImage *uiimage = [UIImage imageWithCIImage:image];
    // 5.开启图形上下文
    UIGraphicsBeginImageContext(uiimage.size);
    // 6.画二维码的图片
    [uiimage drawInRect:CGRectMake(0, 0, uiimage.size.width, uiimage.size.height)];
    // 7.画Logo的图片
    UIImage *meImage = [UIImage imageNamed:@"account_logo"];
    CGFloat meImageW = 50;
    CGFloat meImageH = 50;
    CGFloat meImageX = (uiimage.size.width - meImageW) * 0.5;
    CGFloat meImageY = (uiimage.size.height - meImageH) * 0.5;
    [meImage drawInRect:CGRectMake(meImageX, meImageY, meImageW, meImageH)];
    // 8.获取图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    // 9.关闭图形上下文
    UIGraphicsEndImageContext();
    // 10.给imageView赋值
    return finalImage;
    }
本文总阅读量