重复调用UIGraphicsGetCurrentContext是否有性能损失

发布于 2024-11-08 22:56:14 字数 272 浏览 0 评论 0原文

我正在使用自定义 drawRect 函数构建一个 UIView 。这是一个相当复杂的视图,需要绘制许多不同的项目。我基本上将其分解为每个需要绘制的项目的一个函数。

我想知道的是,我应该将从 UIGraphicsGetCurrentContext() 获取的 CGContextRef 作为参数传递给每个函数,还是可以在每个函数的开头调用它功能?后一个选项对我来说看起来更简洁,但我想知道是否会有很大的性能损失?

I'm building a UIView with a custom drawRect function. This is a fairly complex view, with a number of different items that need to be drawn. I've basically broken it down into one function per item that needs to be drawn.

What I'm wondering is should I pass my CGContextRef, obtained from UIGraphicsGetCurrentContext(), as a parameter to each function, or can I just call it at the start of each function? The latter option looks neater to me, but I am wondering if there is much of a performance penalty?

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

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

发布评论

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

评论(3

等待我真够勒 2024-11-15 22:56:14

除非您要保存/恢复周围的上下文,否则都是一样的。无论如何,从该方法获取上下文很可能永远不会成为瓶颈。

我建议,如果您不保存和恢复状态,可以使用 UIGraphicsGetCurrentContext()。但是,如果您确实要保存状态,则应该通过此选项,因为这样会更容易阅读代码。

我想这是风格问题...

It's the same, unless you are saving/restoring context all around. In any way, getting the context from that method will, most probably, never be the bottleneck.

I suggest that if you are not saving and restoring states, you could use the UIGraphicsGetCurrentContext(). However, if you are indeed saving state, you should pass this one since it would be easier to read your code.

It's a matter of style I guess...

你如我软肋 2024-11-15 22:56:14

Pier-Olivier 的回应很好,而且正好抓住了关键问题:除非必要,否则不要担心。这是一个过早优化的例子。在花费大量时间决定是否传递 CGContextRef 之前,您应该编写应用程序,然后查看性能。使用 Instruments 可以帮助您了解找出你真正的瓶颈在哪里。如果事实证明这导致了问题(我非常怀疑),那么您可以对其进行优化。

Pier-Olivier's response is good, and just grazes the key issue: don't worry about it until you have to. This is a case of premature optimization. Before spending a lot of time deciding whether to pass around your CGContextRef, you should write your application and then look at the performance. Using Instruments can help you figure out where your real bottlenecks are. If it turns out this is causing problems (which I highly doubt), then you can optimize it.

凑诗 2024-11-15 22:56:14

只需在正确实施并经过充分测试后进行分析即可。

如果它确实显示为热点,那么您的问题可能最好分开,和/或渲染到屏幕外上下文......或通过使用较低级别的渲染。

just profile after it's implemented correctly and well tested.

if it really shows up as a hotspot, then your problem is likely best divided, and/or rendered to an offscreen context... or by using lower level rendering.

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