绘制大型 CGPath 最有效的方法是什么?

发布于 2024-07-27 14:00:51 字数 617 浏览 4 评论 0原文

好吧,我有一个 UIView,它显示一个相当宽的 CGPath。 它可以在 UIScrollView 内水平滚动。 现在,我使用 CATiledLayer 来查看视图,因为它的宽度超过 1024 像素。

所以我的问题是:这有效吗?

- (void) drawRect:(CGRect)rect {
    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextAddPath(g,path);
    CGContextSetStrokeColor(g,color);
    CGContextDrawPath(g,kCGPathStroke);
}

本质上,每次绘制图层中的图块时,我都会绘制整个路径。 有谁知道这是否是一个坏主意,或者 CGContext 相对聪明地只绘制剪切矩形内的路径部分?

路径主要是以这样的方式设置的,我可以将其分成大小和形状与图块相似的块,但这需要我做更多的工作,需要路径之间有一些冗余(对于跨越图块边界),并且还需要一些计算来找到要绘制的一条或多条路径。

朝这个方向移动是否值得,或者 CGPath 已经绘制得相对较快了?

Alright, I have a UIView which displays a CGPath which is rather wide. It can be scrolled within a UIScrollView horizontally. Right now, I have the view using a CATiledLayer, because it's more than 1024 pixels wide.

So my question is: is this efficient?

- (void) drawRect:(CGRect)rect {
    CGContextRef g = UIGraphicsGetCurrentContext();
    CGContextAddPath(g,path);
    CGContextSetStrokeColor(g,color);
    CGContextDrawPath(g,kCGPathStroke);
}

Essentially, I'm drawing the whole path every time a tile in the layer is drawn. Does anybody know if this is a bad idea, or is CGContext relatively smart about only drawing the parts of the path that are within the clipping rect?

The path is mostly set up in such a way that I could break it up into blocks that are similar in size and shape to the tiles, but it would require more work on my part, would require some redundancy amongst the paths (for shapes that cross tile boundaries), and would also take some calculating to find which path or paths to draw.

Is it worth it to move in this direction, or is CGPath already drawing relatively quickly?

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

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

发布评论

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

评论(1

东京女 2024-08-03 14:00:51

Quartz 必须根据它所绘制的 CGContext 的尺寸来裁剪它所绘制的所有内容。 但如果您只发送该图块中可见的几何图形,它仍然会节省 CPU。 如果您通过准备多个路径(每个图块一个)来做到这一点,那么您所说的是一次剪辑(当您创建多个路径时),而不是 Quartz 每次绘制图块时都会进行一次剪辑。 效率增益将取决于您的路径的复杂程度:如果是一些简单的形状,没什么大不了的;如果是,则没什么大不了的。 如果它是国家公路网的矢量绘制地图,它可能会很大! 当然,您必须以这种加速与代码复杂性的增加进行权衡。

在你疯狂优化之前,你可以做的是使用工具来查看在 Quartz 上花费了多少时间。

By necessity Quartz must clip everything it draws against the dimensions of the CGContext it's drawing into. But it will still save CPU if you only send it geometry that is visible within that tile. If you do this by preparing multiple paths, one per tile, you're talking about doing that clipping once (when you create your multiple paths) versus Quartz doing it every time you draw a tile. The efficiency gain will come down to how complex your path is: if it's a few simple shapes, no big deal; if it's a vector drawn map of the national road network, it could be huge! Of course you have to trade this speedup off against the increased complexity of your code.

What you could do is use instruments to see how much time is being spent in Quartz before you go crazy optimizing stuff.

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