PlatformThinking 2017-12-03
感谢大佬之前的文章让我改善了手写涂鸦的性能:
刚好我最近在写的项目有一个手写签名的需求,自己照着复现了下,但是有一个手写板的内容输出的需求,项目之前有个方法生成纯色图片:
根据颜色生成纯色图片
@param color 颜色
@return 纯色图片
*/
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//开启上下文
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}于是想用这个方法,直接将当前的手写板的内容输出:
- (UIImage*) imageWithUIView:(UIView*) view{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(view.bounds.size);
CGContextRef currnetContext = UIGraphicsGetCurrentContext();
[view.layer renderInContext:currnetContext];
// 从当前context中创建一个改变大小后的图片
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
return image;
}发现这个func的输出会失真(测试机:IP7)后来找到了解决失真模糊的方法:
-(UIImage *)convertViewToImage:(UIView *)view{
CGSize size = view.bounds.size;
//第一个参数表示区域大小
//第二个参数表示是否非透明的:半透明效果-NO
//第三个参数表示屏幕密度
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}运行时输出的内存消耗(之前我以为会比较吃内存==嘤嘤嘤,但是发现内存几乎没有波动)。