是否可以在带有CGPath的UIView中绘制路径并将其导出为PNG?
答案 0 :(得分:3)
假设你想要一个UIImage而不是一个png文件,你可以这样做:
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextSetLineCap(context, kCGLineCapSquare);
//DRAW YOUR PATH HERE
CGContextStrokePath(context);
myUIImage = UIGraphicsGetImageFromCurrentImageContext();
[myUIImage retain];
UIGraphicsEndImageContext();
答案 1 :(得分:1)
最干净的方法是在UIGraphicsBeginImageContext()
生成的图像上下文中再次执行整个绘图,从中获取UIImage
,然后通过UIImagePNG/JPEGRepresentation()
函数保存它
请注意,UIView不会“保留”图像。你可以重新渲染一个UIView的图层,但它严重违反了MVC(你正在使用视图来存储模型数据!),它看起来并不干净。