CIFilter创建二维码

创建一个ImageView展示二维码

    
    self.imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.width/2.0)];
    
    [self.view addSubview:_imgView];
       
    
    _imgView.image= [self createQRCodeWithString:@"www.baidu.com"];

    _imgView.layer.shadowOffset=CGSizeMake(0, 0.5);//设置阴影的偏移量

    _imgView.layer.shadowRadius=1;//设置阴影的半径

    _imgView.layer.shadowColor=[UIColor blackColor].CGColor;//设置阴影的颜色为黑色

    _imgView.layer.shadowOpacity=1;

- (UIImage *)createQRCodeWithString:(NSString *)qrString{
    //二维码滤镜
    
    CIFilter *filter=[CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //恢复滤镜的默认属性
    
    [filter setDefaults];
    
    //将字符串转换成NSData
    
    NSData *data=[qrString dataUsingEncoding:NSUTF8StringEncoding];
    
    //通过KVO设置滤镜inputmessage数据
    
    [filter setValue:data forKey:@"inputMessage"];
    // 设置质量
    [filter setValue:@"Q" forKey:@"inputCorrectionLevel"];
    //获得滤镜输出的图像
    
    CIImage *outputImage=[filter outputImage];
    
    UIImage *image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:100.0];
    
    return image;
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 创建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);
    
    // 保存bitmap到图片
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    CGContextRelease(bitmapRef);
    
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaledImage];
    
}
CIFilter创建二维码_第1张图片
Paste_Image.png

Swift版


    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        view.addSubview(imageView)
        imageView.frame = CGRect(x: 50, y: 100, width: 200, height: 200);
        imageView.image = createQRImage("www.baodd.com")
        
        
        imageView.layer.shadowOffset=CGSizeMake(0, 0.5);//设置阴影的偏移量
        
        imageView.layer.shadowRadius=1;//设置阴影的半径
        
        imageView.layer.shadowColor = UIColor.blackColor().CGColor;//设置阴影的颜色为黑色
        
        imageView.layer.shadowOpacity=1;
    }

  
    private func createQRImage(qrString:String) -> UIImage{
    
        let filter = CIFilter(name: "CIQRCodeGenerator")
        
        filter?.setDefaults()
        
        let stringData = qrString.dataUsingEncoding(NSUTF8StringEncoding)
        
        filter?.setValue(stringData, forKey: "inputMessage")
        filter?.setValue("Q", forKey: "inputCorrectionLevel")
        
        let outPutImage = filter?.outputImage
        
        
        
        let qrImage = createNonInterpolatedUIImageFormCIImage(outPutImage!, size: 200)
        
        let iconImage = UIImage.init(named: "1111")
        
        let finalImage = creteImage(qrImage, iconImage: iconImage!)
        
        return finalImage
        
    }
    /**
     合成图片
     */
    private func creteImage(bgImage: UIImage, iconImage: UIImage) -> UIImage
    {
       
        UIGraphicsBeginImageContext(bgImage.size)
      
        bgImage.drawInRect(CGRect(origin: CGPointZero, size: bgImage.size))
        
        let width:CGFloat = 50
        let height:CGFloat = width
        let x = (bgImage.size.width - width) * 0.5
        let y = (bgImage.size.height - height) * 0.5
        iconImage.drawInRect(CGRect(x: x, y: y, width: width, height: height))
       
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
    
        return newImage
    }
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()!
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }
    private lazy var imageView: UIImageView = {
    
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.orangeColor()
        return imageView
    }()

CIFilter创建二维码_第2张图片
Paste_Image.png

你可能感兴趣的:(CIFilter创建二维码)