生成二维码

为什么没和条形码一起写?是因为之前项目中都用到了,但是并不是从一个地方寻找到的方法,并且之前的条形码方法是传入宽高和内容直接返回的图片,这个二维码的方法是传入已经建立好的UIImageView和二维码内容与清晰度获得一个二维码。


//调用生成方法(参数1.UIImageView 参数2.二维码内容 参数3.清晰度Scale数值越高越清晰

[self barImageWithImageView:imageView String:@"http://www.baidu.com"Scale:4];


//生成方法

- (void)barImageWithImageView:(UIImageView*)imageView String:(NSString*)str Scale:(CGFloat)scale{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    
    NSData *data = [str 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:1.0
                                  orientation:UIImageOrientationUp];
    
    UIImage *resized =nil;
    CGFloat width = image.size.width*scale;
    CGFloat height = image.size.height*scale;
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef context1 =UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context1,kCGInterpolationNone);
    [image drawInRect:CGRectMake(0,0, width, height)];
    resized = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    imageView.image = resized;
    CGImageRelease(cgImage);
}


感谢观看,学以致用更感谢!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值