2012-04-02 41 views
0

我需要绘制一个饼图。我在这里找到了一个很好的教程http://mac-objective-c.blogspot.com/2009/04/drawing-pie-charts.htmlUIBezierPath到UIImage

该示例使用NSBezierPath,但我使用UIBezierPath,因为我需要IOS中的图形。

有没有办法在UIImage中获取图形,以便我可以在UIImageView中显示它?

在本教程中http://blog.gafmediastudio.com/2010/07/02/draw-a-pie-chart-with-iphone-ipod-ipad/他们使用CGContext,最后他们有一个函数返回一个UIImage。

UIBezierPath类似乎更容易使用,所以我怎么能在UIImage中得到该图形?从我读过的改变drawRect不是很推荐。至少,不是为了我想要做的。

谢谢!

回答

3

首先为您的屏幕拍照并裁剪所需尺寸的图像。

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

最终我在帖子中使用了第二个链接来实现我的目标。不管怎么说,还是要谢谢你。 – 2012-05-01 09:07:33