ios - IOS 上的图像缓存

标签 ios objective-c memory allocation

我的应用内存有问题。我检查了 Instruments 以获得关于这个问题的更多线索,我发现我 79% 的内存被这个占用了:

instruments

所以我在谷歌上搜索了一下,有人说这是图像缓存,它把我所有的图像都保存在内存中。也许它来 self 的分配?

这是我如何称呼我的图像:

info = [InfoModel getInfo:[NSString stringWithFormat:@"%d", self.idEnigme]];

    myImage = [UIImage imageNamed:[NSString stringWithFormat:@"res/img/%@", [info objectForKey:@"path1"]]];
    myImageView = [[UIImageView alloc] initWithImage:myImage];

    myImageView.frame = CGRectMake(0, 200, [[UIScreen mainScreen] bounds].size.width, 400);

    [self.scrollView addSubview:myImageView];

Info 是一个类,我在其中解析 Json 文件,其中是我的图像路径。

感谢您的帮助,这让我发疯。

最佳答案

当您调用 imageNamed:

时,iOS 会自动缓存您的图像以备将来使用

正如在几个地方所讨论的,包括这里:Does UIImageView cache images?

如果您知道您只打算创建一次缓存,则可以绕过此缓存

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]

代替

关于ios - IOS 上的图像缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22539104/

相关文章:

ios - 嵌入 2 个动态 UITableView

ios - Swift 的编码/解码枚举 (Xcode 6.1)

objective-c - 如何在库或框架中禁用 NSLog?例如Admob 库

iphone - UIButton 和 UIControlEventState 问题

iphone - MKReverseGeocoder 委托(delegate)方法是否在单独的线程上工作?

c - 来自 valgrind 的未初始化值

ios - Xcode 9 Swift 4 UITableView 无法识别的选择器发送到实例

ios - XCode中带有环境变量的条件链接静态库

许多 goroutine 的内存消耗

PHP SimpleXML 大文件没有额外的内存使用