iPhone - 大多数内存有效的初始化图像方式? [英] iPhone - Most memory effecient way to initialize an image?

查看:123
本文介绍了iPhone - 大多数内存有效的初始化图像方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过imageNamed:在尝试初始化图像时很糟糕。但那么最好的方法是什么?我正在使用imageWithContentsOfFile:并在我的资源文件夹中传递图像的路径

I've read that imageNamed: is bad when trying to initialize images. But then what is the best way? I am using imageWithContentsOfFile: and passing the path of an image in my resources folder

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]

此调用在for循环中进行了大约30次。

This call is made some 30 times in a for loop.

现在当我使用乐器运行我的应用程序时,我发现NSString已经耗尽了大量内存用于上面的操作,我们使用字符串文字(@ jpg)
仪器将负责的调用者显示为[NSBundle mainBundle],当我使用该类型的字符串文字时,这又指向该行。

Now when I run my app with instruments, I see that a lot of memory is used up by NSString for operations like the above one where we use string literals (@"jpg") Instruments shows the responsible caller as [NSBundle mainBundle] and this in turn points to the line when I use the string literal for the type.

那么在不使用太多内存的情况下初始化图像的最有效方法是什么?

So what is the most effective way of initializing images without using too much of memory?

我将语句更改为

img = [UIImage imageWithContentsOfFile:[bndl pathForResource:fileName ofType:extn]]

其中 extn 是静态的并初始化为 @jpg fileName 为for循环的每次迭代不断更改。但即便如此,最大限度地使用 NSString 也是因为 [NSBundle mainBundle] [NSBundle pathForResource:OfType:] 根据Instruments。

where extn is static and initialized to @"jpg". fileName keeps changing for each iteration of the for loop. But even then the maximum use of NSString is because of [NSBundle mainBundle] and [NSBundle pathForResource:OfType:] according to Instruments.

推荐答案

我会避免在你使用自动释放的对象可以在循环内。如果Instruments在NSBundle pathForResource:ofType:call上报告大量点击,我会在循环外部进行一些处理。

I'd avoid using autoreleased objects where you can within a loop. If Instruments is reporting a lot of hits on the NSBundle pathForResource:ofType: call, I'd pull some of that processing outside of the loop.

我建议的实现看起来如何类似这样的事情:

My suggested implementation would look something like this:

NSString *resourcePath = [[[NSBundle mainBundle] resourcePath] retain];

for (int i = 0; i < 1000; ++i) 
{   
    ...

    NSString *pathForImageFile = [resourcePath stringByAppendingPathComponent:fileName];
    NSData *imageData = [[NSData alloc] initWithContentsOfFile:pathForImageFile];

    UIImage *image = [[UIImage alloc] initWithData:imageData];
    [imageData release];

    ... 

    [image release];
}

[resourcePath release];

你将累积一个自动释放的字符串(pathForImageFile),但这不应该那么糟糕。你可以在循环中创建和释放一个自动释放池,但我建议每10或100次循环通过一次,而不是每次通过。此外,resourcePath上的保留和释放可能是多余的,但我把它放在那里,以防你想在这里的某个地方使用你自己的自动释放池。

You will be accumulating one autoreleased string (pathForImageFile), but that shouldn't be so bad. You could create and release an autorelease pool within the loop, but I would suggest doing that at most once every 10 or 100 loop passes, not every pass. Also, the retain and release on resourcePath may be superfluous, but I put it there in case you want to use your own autorelease pool somewhere in here.

这篇关于iPhone - 大多数内存有效的初始化图像方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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