iOS高清二维码生成以及崩溃注意事项
项目中最近加入了分享二维码功能。毫无疑问当然是选择系统自带的二维码生成方法。
// 1. 创建一个二维码滤镜实例(CIFilter)
-(UIImage *)getACIFilterImage:(NSString *)urlString{ //urlString是要生成的二维码扫描出来的内容
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 滤镜恢复默认设置
[filter setDefaults];
// 2. 给滤镜添加数据
NSString *string = urlString;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKeyPath:@"inputMessage"];
CIImage *image = [filter outputImage];
//如果生成的二维码比较模糊,更改下面数值可以变高清
CGAffineTransform transform = CGAffineTransformMakeScale(2.0f, 2.0f); // Scale by 5 times along both dimensions
CIImage *output = [image imageByApplyingTransform: transform];
UIImage *newImage = [UIImage imageWithCIImage:output scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
return newImage;//返回生成的高清二维码
}
注意:我在项目中遇到了下面崩溃
0 CoreImageCI::GLContext::init() + 2761 CoreImageCI::GLContext::init() + 2402 CoreImageCI::GLContext::GLContext(CI::GLContext::ShareContextInfo, CGColorSpace*, CGColorSpace*, CI::PixelFormat, bool, unsigned long, bool, bool) + 4163 CoreImageCI::GLContext::GLContext(CI::GLContext::ShareContextInfo, CGColorSpace*, CGColorSpace*, CI::PixelFormat, bool, unsigned long, bool, bool) + 244 CoreImage+[CIContext(Internal) internalContextWithEAGLContext:options:] + 7565 CoreImage-[CIContext initWithOptions:] + 8206 CoreImage+[CIContext contextWithOptions:] + 527 UIKit-[UIImageView _updateLayerContentsForCIImageBackedImage:] + 5808 UIKit-[UIImageView _setImageViewContents:] + 8809 UIKit-[UIImageView _updateState] + 66410 UIKit+[UIView(Animation) performWithoutAnimation:] + 10411 UIKit-[UIImageView _updateImageViewForOldImage:newImage:] + 50012 UIKit-[UIImageView setImage:] + 348
崩溃的原因是生成的二维码内存过大,修改上面的高清比例。
2另一个处理二维码清晰度的方法
/** 根据CIImage生成指定大小的UIImage */+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size { CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 1.创建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); // 2.保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage];}
崩溃日志,也是因为二维码过大内存问题引起的。
0 libGPUSupportMercury.dylibgldCreateContext + 1801 libGPUSupportMercury.dylibgldCreateContext + 1802 GLEnginegliCreateContextWithShared + 6963 OpenGLES-[EAGLContext commonInitWithAPI:properties:] + 4124 CoreImage_CIEAGLContextCreate + 2605 CoreImageCI::GLContext::GLContext(CI::GLContext::ShareContextInfo, CGColorSpace*, CGColorSpace*, CI::PixelFormat, bool)+ 2526 CoreImage+[CIContext(Internal) internalContextWithEAGLContext:options:] + 3927 CoreImage-[CIContext initWithOptions:] + 7048 CoreImage+[CIContext contextWithOptions:] + 529 JINJIANG-iOS-[TopView createNonInterpolatedUIImageFormCIImage:withSize:] (TopView.m:0)、
如果想测试上述两个原因是否是内存过大引起的可以吧比例弄成很大。例如500.0f