Objective C 显示图像数组

发布于 2024-12-17 01:24:00 字数 1029 浏览 0 评论 0原文

所以我正在创建一个应该有动态生成的珊瑚礁的游戏。珊瑚礁有几个包含我尝试显示的图像 (100x100) 的对象。我可以显示图像,但 CPU 使用率很高。我相信问题出在我的绘图代码中:

-(void)drawCoralWithContext:(CGContextRef)ctx{
    for(Coral *c in coral){
        CGRect coralRect = CGRectMake (0, 0, [c size], [c size]);

        CGLayerRef layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        CGContextRef layerCtx = CGLayerGetContext(layerRef);
        coralRect = CGRectMake (0, 0, [c size], [c size]);
        layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        layerCtx = CGLayerGetContext(layerRef);
        CGContextDrawImage(layerCtx, coralRect, [c image]);
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, c.position.x, c.position.y);
        CGContextDrawLayerAtPoint(ctx, [c position], layerRef);
        CGContextRestoreGState(ctx);
        CFRelease(layerRef);
    }
}

传入的上下文: CGContextRef ctx = UIGraphicsGetCurrentContext();

我需要减少处理量。非常感谢任何帮助,谢谢。

So I'm creating a game that is supposed to have a dynamically generated coral reef. The reef has several objects that contain images (100x100) that I am trying to display. I can display the images but with severe CPU usage. The problem lies in my drawing code I believe:

-(void)drawCoralWithContext:(CGContextRef)ctx{
    for(Coral *c in coral){
        CGRect coralRect = CGRectMake (0, 0, [c size], [c size]);

        CGLayerRef layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        CGContextRef layerCtx = CGLayerGetContext(layerRef);
        coralRect = CGRectMake (0, 0, [c size], [c size]);
        layerRef = CGLayerCreateWithContext(ctx, CGSizeMake([c size], [c size]), NULL);
        layerCtx = CGLayerGetContext(layerRef);
        CGContextDrawImage(layerCtx, coralRect, [c image]);
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, c.position.x, c.position.y);
        CGContextDrawLayerAtPoint(ctx, [c position], layerRef);
        CGContextRestoreGState(ctx);
        CFRelease(layerRef);
    }
}

The context being passed in:
CGContextRef ctx = UIGraphicsGetCurrentContext();

I need to reduce the amount of processing. Any help is greatly appreciated, thanks.

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

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

发布评论

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

评论(1

走野 2024-12-24 01:24:00

您看过 CATiledLayer 吗?它的文档不是很好,但是为绘制充满许多小图像的屏幕提供了很多性能改进。有一些来自旧 WWDC 的示例代码,您可能想查看本教程 http: //www.cimgf.com/2011/03/01/subduing-catiledlayer/

Have you looked at CATiledLayer? Its documentation is not very good but offers a lot of performance improvement for drawing a screen full of many small images. There's some sample code from old WWDC's and you might want to check out this tutorial http://www.cimgf.com/2011/03/01/subduing-catiledlayer/

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