二维码生成

黑白二维码

//黑白二维码生成
-(void)demo1{
    //CoreImage包含在UIKit框架中
    //获取内置的所有过滤器
    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    NSLog(@"%@",filters);
    //1.创建二维码过滤器
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //2.设置默认值
    [qrFilter setDefaults];
    //3.打印输入的key
    NSLog(@"%@",qrFilter.inputKeys);
    /*
     inputMessage, 二维码信息
     inputCorrectionLevel 容错水平
     */
    
    //4.设置二维码的信息
    
    [qrFilter setValue:[@"无聊" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
    //5.输出二维码图片
    CIImage *ciImage = qrFilter.outputImage;
    NSLog(@"%@",ciImage);
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    self.imageView.image = [UIImage imageWithCIImage:ciImage];
}

彩色二维码

//彩色二维码
-(void)demo2{
    //CoreImage包含在UIKit框架中
    //获取内置的所有过滤器
    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    NSLog(@"%@",filters);
    //1.创建二维码过滤器
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //2.设置默认值
    [qrFilter setDefaults];
    //3.打印输入的key
    NSLog(@"%@",qrFilter.inputKeys);
    /*
     inputMessage, 二维码信息
     inputCorrectionLevel 容错水平
     */
    
    //4.设置二维码的信息
    
    [qrFilter setValue:[@"无聊" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
    //5.输出二维码图片
    CIImage *ciImage = qrFilter.outputImage;
    NSLog(@"%@",ciImage);
    //6.获取颜色过滤器
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"];
    //7.设置默认值
    [colorFilter setDefaults];
    NSLog(@"%@",colorFilter.inputKeys);
    /*
     inputImage 需要改变颜色的图片
     inputColor0, 前景色
     inputColor1  背景色
     */
    [colorFilter setValue:ciImage forKey:@"inputImage"];
    [colorFilter setValue:[CIColor colorWithRed:1 green:0 blue:0] forKey:@"inputColor0"];
    [colorFilter setValue:[CIColor colorWithRed:0 green:1 blue:0] forKey:@"inputColor1"];
    ciImage = colorFilter.outputImage;
    
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    self.imageView.image = [UIImage imageWithCIImage:ciImage];

}

二维码添加头像

//二维码添加头像
-(void)demo3{
    //CoreImage包含在UIKit框架中
    //获取内置的所有过滤器
    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    NSLog(@"%@",filters);
    //1.创建二维码过滤器
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //2.设置默认值
    [qrFilter setDefaults];
    //3.打印输入的key
    NSLog(@"%@",qrFilter.inputKeys);
    /*
     inputMessage, 二维码信息
     inputCorrectionLevel 容错水平
     */
    
    //4.设置二维码的信息
    
    [qrFilter setValue:[@"无聊" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
    //5.输出二维码图片
    CIImage *ciImage = qrFilter.outputImage;
    NSLog(@"%@",ciImage);
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformMakeScale(10, 10)];
    //6.开启图形上下文
    UIImage *uiImage = [UIImage imageWithCIImage:ciImage];
    UIGraphicsBeginImageContext(uiImage.size);
    //7.画二维码图片
    [uiImage drawInRect:CGRectMake(0, 0, uiImage.size.width, uiImage.size.height)];
    //8.画自定义图片
    UIImage *image = [UIImage imageNamed:@"picture"];
    CGFloat imageW = 100;
    CGFloat imageH = 100;
    CGFloat imageX = (uiImage.size.width - imageW) * 0.5;
    CGFloat imageY = (uiImage.size.height - imageH) * 0.5;
    [image drawInRect:CGRectMake(imageX, imageY, imageW, imageH)];
    //9.获取当前上下文中图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    //10.关闭图形上下文
    UIGraphicsEndImageContext();
    
    self.imageView.image = finalImage;

}

你可能感兴趣的:(二维码生成)