问题
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