简体   繁体   中英

UIBezierPath to UIImage

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!

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];

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