【问题标题】:Image Optimization (iOS)图像优化 (iOS)
【发布时间】:2013-12-29 13:25:07
【问题描述】:

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

我看过下面的方法会占用大量内存

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 中保存为网络。

【问题讨论】:

  • “我读过以下方法使用大量内存”本身并不正确。此外,这与 Xcode 无关。

标签: ios image ipad memory optimization


【解决方案1】:

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

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

使用[[UIImage alloc] initWithContentsOfFile:path] 图像不会被缓存,您可以通过调用[image release] 或使用ARC 将属性设置为nil 来“强制”释放内存。您可以更好地管理何时释放内存

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多