Objective C 显示图像数组
所以我正在创建一个应该有动态生成的珊瑚礁的游戏。珊瑚礁有几个包含我尝试显示的图像 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看过 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/