2013-12-11 95 views
0

我正在制作iPad杂志,并且使用了大量图像(背景,幻灯片和动画),并且使用的内存非常高。图像优化(iOS)

我读过下面的方法使用了大量的内存

UIImage *picture = [UIImage imageNamed:@"myFile.png"]; 

他们建议使用这一个

NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/myFile.png"]; 
imageView.image = [UIImage imageWithContentsOfFile:fullpath]; 

但我发现了另一个方法以及

imageView.image = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"png"]]; 

为了优化我的应用程序,我应该使用哪种方法?我的所有图像都是.jpg,并保存为Photoshop中的网页。

+0

更好的管理“我读过下面的方法使用的内存了很多”是不正确的本身。另外,这与Xcode无关。 – 2013-12-11 14:14:42

回答

2

所有3种方法将使用相同数量的内存。差异如下:

使用[UIImage imageNamed:@"myFile.png"]图像缓存在内存中以加快重用。这对于在应用程序中使用多次的小图像很有用(图像背景等)。收到内存警告时,高速缓存将被删除以用于未使用的映像。

使用[[UIImage alloc] initWithContentsOfFile:path]图像未被缓存,您可以通过调用[image release]或使用ARC将属性设置为零来“强制”释放内存。你有当内存被释放

使用[UIImage imageWithContentsOfFile:fullpath]只是相当于[[[UIImage alloc] initWithContentsOfFile:path]autorelease]