Quartz 2D 图形上下文

发布于 2024-08-15 10:53:09 字数 811 浏览 5 评论 0原文

为什么 Quartz 2D Graphics Contexts 函数必须从 drawRect 方法中调用?

因为如果我从除 drawRect 之外的任何地方调用 CGGraphics 上下文函数,我都会收到如下消息:

<Error>: CGContextFillRects: invalid context
<Error>: CGContextSetFillColorWithColor: invalid context

实际上,在 UIView 的子类中,我确实在名为 Render 的方法中设置了图形上下文。打赌,当调用 Render 时,我会收到上述错误:

- (void)Render {
    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(g, [UIColor blueColor].CGColor);
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    [@"It works!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    NSLog(@"gsTest Render");
}

Why do Quartz 2D Graphics Contexts functions have to be called from within the drawRect method?

Because if I call a CGGraphics context function from anywhere except from within drawRect I get messages like this:

<Error>: CGContextFillRects: invalid context
<Error>: CGContextSetFillColorWithColor: invalid context

Actually in a subclass of UIView I do setup a Graphics Context in a method called Render. Bet when Render is called I get the above errors:

- (void)Render {
    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(g, [UIColor blueColor].CGColor);
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    [@"It works!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
    NSLog(@"gsTest Render");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-08-22 10:53:09

他们不这样做。也许你应该详细说明一下。

Cocoa 在调用您的drawRect 实现之前为您设置一个上下文。如果你想在其他地方画一些东西,那么设置工作就是你的责任。

They don't. Maybe you should elaborate a little more.

Cocoa sets up a context for you before calling your drawRect implementation. If you want to draw something somewhere else then that setup work is your responsibility.

妳是的陽光 2024-08-22 10:53:09

UIGraphicsGetCurrentContext 从框架设置的堆栈中检索图形上下文。 UIKit 保证当调用 drawRect 方法时,有效的图形上下文已被推送到此堆栈上。从它返回后,该堆栈将被弹出。如果在drawRect函数之外调用它,它将无效。

相反,如果您想在drawRect之外调用它,您需要创建/获取您自己的图形上下文并在其上进行绘制。

一些绘图函数,例如 NSString drawAtPoint:withFont: 也使用这个堆栈;如果当前上下文无效,您将需要调用 UIGraphicsPushContext

UIGraphicsGetCurrentContext retrieves the graphics context from a stack which is set up by the framework. UIKit guarantees that when the drawRect method is called, a valid graphics context has been pushed onto this stack. After you return from it, this stack gets popped. If you call it outside the drawRect function it will not be valid.

Instead, if you want to call it outside drawRect, you need to create/obtain your own graphics context and draw onto that.

Some drawing functions, such as the NSString drawAtPoint:withFont: make use of this stack also; if the current context is not valid you will need to call UIGraphicsPushContext

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文