簡體   English   中英

將CGPath導出為JPG或PNG

[英]Export CGPath as JPG or PNG

是否可以在帶有CGPath的UIView中繪制路徑並將其導出為PNG?

假設您想要的是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();

最干凈的方法是在由UIGraphicsBeginImageContext()生成的圖像上下文中再次執行整個繪圖,從中獲取UIImage ,然后通過UIImagePNG/JPEGRepresentation()函數將其保存。

請注意,UIView不會“保留”圖像。 您可以重新渲染 UIView的圖層,但這完全違反了MVC(您正在使用視圖來存儲模型數據!),對我而言,它看起來並不干凈。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM