-5

我使用了 imageNamed 但 imageNamed 会缓存您的图像,但是使用 imageWithContentsOfFile: 来避免缓存。

我发现使用仪器会消耗大量时间。这怎么可能更好?

在此处输入图像描述

4

3 回答 3

3

You may be misunderstanding what Instruments profiling is telling you. 95% of 4.3% is not very much. It all depends on what else is happening. If you are not experiencing a serious slowdown, don't optimize unnecessarily!

A question to ask yourself, though, is whether you really need an array of physical image data. It really does take serious time to decompress an image file into its UIImage data bitmap. (Not to mention that you can run yourself out of memory this way.) Why not store an array of strings (your pathForResource strings) and fetch the image only when needed?

Finally I would point out that I see a pattern here: You are fetching image 13, then image 12, then image 11, and so on. So your code that does this manually is very silly, since if you tell me the index number I can tell you algorithmically what image corresponds to it.

于 2013-03-21T18:13:24.807 回答
0

Try to replace using [NSBundle mainBundle] pathForResource:... with this code:

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];

for example get some image file path

UIImage *img= [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@\myImage.png",bundleRoot]];
于 2013-03-21T18:13:17.183 回答
0

您正在读取所有这些文件,解压缩它们,为未压缩版本分配内存,并将它们存储在数组中。你需要所有这些吗?您可以按需加载它们而不是一次全部加载吗?这将减少初始化数组导致的重大影响。

此外,请确保您的图像不大于实际显示的大小。如果您加载大图像只是为了在小区域显示它,则加载和解压缩该大图像将花费更长的时间。

于 2013-03-21T18:11:42.900 回答