简体   繁体   中英

Export CGPath as JPG or PNG

是否可以在带有CGPath的UIView中绘制路径并将其导出为PNG?

Assuming you want a UIImage, not a png file, you can do something like this:

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();

The cleanest way to do that is to perform the whole drawing again in an image context produced by UIGraphicsBeginImageContext() , get an UIImage out of it, then save it via the UIImagePNG/JPEGRepresentation() functions.

Note that UIView do not "hold" images. You can rerender a UIView's layer, but it's a gross violation of MVC (you're using views to store model data!), and it doesn't look clean to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM