2012-04-02 43 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