在iPad上优化加载许多图像 [英] Optimize loading of many images on iPad

查看:108
本文介绍了在iPad上优化加载许多图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iPad项目,我试图通过切换预渲染图像来模拟3D体的旋转。

I'm working on a project for the iPad where I'm trying to simulate the rotation of a 3D-body with touch by switching pre-rendered images.

在内存管理方面,我不是最敏锐的人,所以我想知道是否有人对如何优化这一点有任何提示。

I'm not the sharpest man when it comes to memory management so I wonder if anyone has any tips on how to optimize this.

我的解决方案现在看起来像这样:

My solution now looks something like this:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
CGPoint location = [myTouch locationInView:self.view];
int pictureIndex;
//Rough mapping of image to point on screen
if (location.x <= 384 ) {
    pictureIndex = (384 - location.x)/(768/rotatingPictures)+1;
}
else {
    pictureIndex = rotatingPictures - (location.x-384)/(768/rotatingPictures)+1;
}
[theImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image_%d.png", pictureIndex]]];

}

加载是否有效这样的图像?将图像首先加载到数组或类似的东西会更好吗?

Is it efficient to load the images this way? Would it be better to load the images to an array first or something like that?

我希望一次加载所有加载时间,而不是在旋转时加载。

I would prefer to have all loading time at once and not while rotating.

推荐答案

第一个关键信息是 imageNamed无望 !众所周知,它是一只狗。这既不是一件事,也不是另一件事;它不能很好地缓存,并且往往会占用太多的内存。 (Google上有100篇文章。) 所以,那就是

The first critical piece of information is that imageNamed is hopeless! It is well-known to be a dog. It's neither one thing or the other; it doesn't cache well and it tends to build up too much memory use. (Google up 100s of articles on this.) So, that's out.

你必须告诉我们确切地说你有多少张图片,以及每张图片究竟有多大(KB或MB)。

You will have to tell us exactly how many images you have, and exactly how big (KB or MB) each image is.

只有这样才能决定一个成功的策略。

Only then can one decide a successful strategy.

要正常从一个大图像切换到另一个大图像,如果你反复交换大图像,如果你不得不担心内存,你可以这样做。

To swap from one large image to another "properly", if you are swapping huge images over and over and if you have to worry about memory, you do this...

[hugeImage.image release];
hugeImage.image = [[UIImage alloc] initWithContentsOfFile:
           [[NSBundle mainBundle]
           pathForResource:@"bodyPart"
           ofType:@"png"]];

将正确摆脱上一张图片并引入下一张图片。永远,永远使用imageNamed - 它专门设计用于小型(即图标大小)界面使用(即按钮,图标等)图像。

that will properly get rid of the previous image and bring in the next one. NEVER, EVER use imageNamed -- it is specifically designed only for small (i.e. icon sized) interface-use (i.e., buttons, icons, etc) images.

然而再次在我们知道图像的精确数量和精确尺寸之前,没有人可以帮助您,实际上每秒或每分钟您想要交换图像的频率。希望它有所帮助!

However again, nobody can help you until we know the precise number of images, and the precise size, and indeed how often per second or per minute you expect the images to be swapped. Hope it helps!

-----你继续说...

----- You went on to say ...

89。 png图片大小从大约70 K到100 K

"89 .png pictures ranging in size from about 70 K to 100 K"

好消息,你将 没有任何问题 即可。你可以很容易地加载它们,你甚至可以实时快速地制作20到30 fps的动画。

Good news, you're going to have no problem whatsoever. You can easily load those, and you can even do it fast enough in real time to make a 20 - 30 fps animation.

完全按照我上面的说法完成每次你从一个交换到另一个,你就完成了。

Simply do exactly what I say above each time you swap from one to another, and you're done.

(对于记录,记得在初始化时首先加载一个,以便上面的代码片段工作你知道,正确地使用第一次。如果可能的话,在你完成后释放最后一个。)干杯。

(For the record, remember to load one first when you are initialising, so that the code fragment above works properly the "first time" it is used, you know. And if possible, release the last one when you're done.) Cheers.

这篇关于在iPad上优化加载许多图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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