缓存到 CGLayer 比在 iPhone 上重绘慢

发布于 2024-09-09 06:25:08 字数 1207 浏览 2 评论 0原文

我正在为 iPhone 制作一个简单的绘图程序。目前,触摸事件将其位置添加到一个列表中,该列表与一堆 CGContextAddLineToPoint 调用连接,这些调用在每次调用 drawRect 时都会重绘。

我在行数相当少的情况下遇到了性能问题(尽管它们是透明的),所以我尝试将所有内容绘制到 CGLayer 中。现在,它不再每帧绘制每条线,而是每条线仅绘制一次,并每帧将 CGLayer 绘制到屏幕上。

        CGContextRef backgroundContext = CGLayerGetContext(pathBuffer);
        CGContextSetLineWidth(backgroundContext, 2.0);
        CGContextSetLineCap(backgroundContext, kCGLineCapButt);
        CGContextSetStrokeColorWithColor(backgroundContext, [pathColor CGColor]);

        if([[touchPoints objectAtIndex:0] continuesToNextPoint])
        {
            CGContextMoveToPoint(backgroundContext, [[touchPoints objectAtIndex:0] point].x, [[touchPoints objectAtIndex:0] point].y);
            CGContextAddLineToPoint(backgroundContext, [[touchPoints objectAtIndex:1] point].x, [[touchPoints objectAtIndex:1] point].y);
            CGContextStrokePath(backgroundContext);
        }

        //remove it from touchpoints
        [touchPoints removeObjectAtIndex:0];
    }

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLayerAtPoint(context, CGPointMake(0, 0), pathBuffer);

如果有的话,这比我之前做的要慢。我做错了什么吗?我会更好地绘制 UIImage 或其他东西吗?谢谢。

I'm in the process of making a simple drawing program for the iPhone. At the moment, touch events add their location to a list which is connected with a bunch of CGContextAddLineToPoint calls which are redrawn in every call to drawRect.

I'm running into performance issues at fairly low numbers of lines (they are transparent, though), so I tried drawing everything into a CGLayer. Now, instead of drawing each line every frame, it draws each line only once and draws the CGLayer to the screen every frame.

        CGContextRef backgroundContext = CGLayerGetContext(pathBuffer);
        CGContextSetLineWidth(backgroundContext, 2.0);
        CGContextSetLineCap(backgroundContext, kCGLineCapButt);
        CGContextSetStrokeColorWithColor(backgroundContext, [pathColor CGColor]);

        if([[touchPoints objectAtIndex:0] continuesToNextPoint])
        {
            CGContextMoveToPoint(backgroundContext, [[touchPoints objectAtIndex:0] point].x, [[touchPoints objectAtIndex:0] point].y);
            CGContextAddLineToPoint(backgroundContext, [[touchPoints objectAtIndex:1] point].x, [[touchPoints objectAtIndex:1] point].y);
            CGContextStrokePath(backgroundContext);
        }

        //remove it from touchpoints
        [touchPoints removeObjectAtIndex:0];
    }

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawLayerAtPoint(context, CGPointMake(0, 0), pathBuffer);

This, if anything, is SLOWER than what I was doing before. Am I doing something wrong? Would I be better served drawing to a UIImage or something? Thanks.

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

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

发布评论

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

评论(1

风筝在阴天搁浅。 2024-09-16 06:25:08

您应该做的是将 CALayer 插入您的视图之一,更新图层,然后使用图层上已更改的区域的边界调用 setNeedsDisplayInRect: ,这会导致它更新。如果您尝试更新整个屏幕,速度会很慢。

您是否使用 Instruments 来找出哪些部分速度慢?我建议使用它来了解哪些部分会减慢您的速度。

What you should be doing is inserting a CALayer into one of your views, updating the layer, and then calling setNeedsDisplayInRect: with the bounds of the area that has changed on the layer, which causes it to update. If you try to update the entire screen, it's going to be slow.

Are you using Instruments to figure out what parts are slow? I recommend using it to learn about what parts are slowing you down.

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