UIBezierPath to UIImage

送分小仙女□ 提交于 2019-12-12 00:00:03

问题


I need to draw a pie chart. I found a nice tutorial here http://mac-objective-c.blogspot.com/2009/04/drawing-pie-charts.html

The example uses NSBezierPath, but I use UIBezierPath because I need the graphic in IOS.

Is there a way to obtain that graphic in a UIImage so I can show it in a UIImageView?

In this tutorial http://blog.gafmediastudio.com/2010/07/02/draw-a-pie-chart-with-iphone-ipod-ipad/ they use CGContext and in the end they have a function that returns a UIImage.

The UIBezierPath Class seems easier to use, so how could I get that drawing in a UIImage? From what I have read altering drawRect is not very recommended. At least, not for what I am trying to do.

Thank you!


回答1:


First take a picture of your screen and crop the image of your desired size.

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Crop the image
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 44, 1024, 660));
img = [UIImage imageWithCGImage:imageRef];


来源:https://stackoverflow.com/questions/9974036/uibezierpath-to-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!