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

查看:14
本文介绍了如何有效地显示许多图像?(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,从 UIImage 中获取 CGImageRef 并将其设置为这些图层的内容.

  • 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.

此外,您可能希望保留一个 CALayer 池,并在必要时通过隐藏/显示来重用它们.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,更不用说创建 UIView 了,这些都是相当重的对象.

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天全站免登陆