如何有效地显示许多图像? (iPhone编程) [英] How to efficiently show many Images? (iPhone programming)

查看:126
本文介绍了如何有效地显示许多图像? (iPhone编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要像粒子系统这样的东西,所以我做了以下内容:

In my application I needed something like a particle system so I did the following:

当应用程序初始化时,我加载了一个UIImage

While the application initializes I load a UIImage

laserImage = [UIImage imageNamed:@"laser.png"];

UIImage * laserImage在我的控制器接口中声明。现在,每当我需要一个新的粒子时,这个代码就会生成一个:

UIImage *laserImage is declared in the Interface of my Controller. Now every time I need a new particle this code makes one:

// add new Laserimage
UIImageView *newLaser = [[UIImageView alloc] initWithImage:laserImage];
[newLaser setTag:[model.lasers count]-9];
[newLaser setBounds:CGRectMake(0, 0, 17, 1)];
[newLaser setOpaque:YES];
[self.view addSubview:newLaser];
[newLaser release];

请注意图片只有17px * 1px小而model.lasers是一个内部数组要做所有的计算都是从图形输出中分离出来的。所以在我的主绘图循环中,我将所有UIImageView的位置设置为我的model.lasers数组中的计算位置:

Please notice that the images are only 17px * 1px small and model.lasers is a internal array to do all the calculating seperated from graphical output. So in my main drawing loop I set all the UIImageView's positions to the calculated positions in my model.lasers array:

for (int i = 0; i < [model.lasers count]; i++) {
    [[self.view viewWithTag:i+10] setCenter:[[model.lasers objectAtIndex:i] pos]];
}

我将标签增加10,因为默认值为0而我不是想要使用默认标签移动所有视图。

I incremented the tags by 10 because the default is 0 and I don't want to move all the views with the default tag.

因此动画看起来很好,大约有10到20张图像,但在处理大约60张图像时确实很慢。所以我的问题是:有没有办法在没有重新开始OpenGl ES的情况下优化它?

So the animation looks fine with about 10 - 20 images but really gets slow when working with about 60 images. So my question is: Is there any way to optimize this without starting over in OpenGl ES?

推荐答案

正如jeff7和FenderMostro所说,您使用的是高级API(UIKit),使用较低的API(CoreAnimation或OpenGL)可以获得更好的性能。 (cocos2d建立在OpenGL之上)

As jeff7 and FenderMostro said, you're using the high-level API (UIKit), and you'd have better performance using the lower APIs, either CoreAnimation or OpenGL. (cocos2d is built on top of OpenGL)


  • 你最好的选择是使用CALayers而不是UIImageViews,从中获取CGImageRef您的UIImage并将其设置为这些图层的内容。

  • Your best option would be to use CALayers instead of UIImageViews, get a CGImageRef from your UIImage and set it as the contents for these layers.

此外,您可能希望保留一个CALayers池并通过隐藏/显示来重复使用它们。 60张17 * 1像素的CALayers并不多,我一直在用它做数百张而不需要额外的优化。

Also, you might want to keep a pool of CALayers and reuse them by hiding/showing as necessary. 60 CALayers of 17*1 pixels is not much, I've been doing it with hundreds of them without needing extra optimization.

这样,图像就已经解压缩并在视频内存中可用。当使用UIKit时,一切都通过CPU,更不用说创建非常重的UIViews了。

This way, the images will already be decompressed and available in video memory. When using UIKit, everything goes through the CPU, not to mention the creation of UIViews which are pretty heavy objects.

这篇关于如何有效地显示许多图像? (iPhone编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆