從-drawRect:
獲取NSView的CGContext並將其用於以後執行更多繪圖是否安全?在一個簡單的測試,像這樣的:NSView的繪圖上下文
CGContextRef context = NULL;
- (void)drawRect:(NSRect)r
{
if (!context)
context = [[NSGraphicsContext currentContext] graphicsPort];
}
- (void)drawSomething
{
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
CGContextFillRect(context, CGRectMake (0, 0, 100, 100));
CGContextFlush(context);
}
一切似乎當-drawSomething
被調用的工作,但它是保證的背景下會不會改變?你可以看到並可能猜到,我試圖繞過使用-drawRect:
繪圖的標準方式。它適用於許多場合,但更多的程序化繪圖方式會讓我的生活變得更輕鬆。