我想從當前的圖形上下文創建一個UIImage對象。更具體地說,我的用例是用戶可以繪製線條的視圖。他們可能會逐漸繪製。完成後,我想創建一個UIImage來表示他們的繪圖。如何從當前圖形上下文創建UIImage?
這裏是什麼的drawRect:看起來像我現在:
- (void)drawRect:(CGRect)rect
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSaveGState(c);
CGContextSetStrokeColorWithColor(c, [UIColor blackColor].CGColor);
CGContextSetLineWidth(c,1.5f);
for(CFIndex i = 0; i < CFArrayGetCount(_pathArray); i++)
{
CGPathRef path = CFArrayGetValueAtIndex(_pathArray, i);
CGContextAddPath(c, path);
}
CGContextStrokePath(c);
CGContextRestoreGState(c);
}
...其中_pathArray的類型是CFArrayRef的,並填充每touchesEnded時間:被調用。另請注意,drawRect:可能會在用戶繪製時被調用多次。
當用戶完成後,我想創建一個代表圖形上下文的UIImage對象。有關如何做到這一點的任何建議?
雙雙跌破工作的答案。請注意,兩者之間存在功能差異,即在圖像上下文中渲染圖層也會渲染圖層的背景;調用drawRect:直接不(在我的情況下沒關係)。我正在使用CALayer的renderInContext方法,因爲直接調用drawRect非常複雜(詳情請參閱http://lists.apple.com/archives/Cocoa-dev/2002/Apr/msg01846.html)。謝謝! – figelwump 2009-07-11 17:41:45