为什么没和条形码一起写?是因为之前项目中都用到了,但是并不是从一个地方寻找到的方法,并且之前的条形码方法是传入宽高和内容直接返回的图片,这个二维码的方法是传入已经建立好的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);
}
感谢观看,学以致用更感谢!