gpt4 book ai didi

objective-c - 我如何解决 iphone 中 imageWithContentsoffile 函数的内存问题

转载 作者:可可西里 更新时间:2023-11-01 06:16:21 26 4
gpt4 key购买 nike

我在将 UIButton 添加到 UITableView 时遇到内存问题。下面是我设置 UIButton 图像的代码:

UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];

但是当我使用 imageName: 方法而不是 imageWithContentsOfFile: 方法时,它工作得很好。有人对此问题有好的解决方案吗?

最佳答案

imageWithContentsOfFile:imageNamed:

据我所知,

imageNamed 在特殊的系统缓存中加载图像,然后使用该图像路径的 future 调用将返回缓存中的图像,而不是从磁盘重新加载它。

imageWithContentsOfFile 只是在您指定的路径加载图像,但不进行缓存。对同一图像多次调用 imageWithContentsOfFile 将导致内存中有多个副本。

关于内存泄漏,我不确定在编程中使用大量图像时,两者哪个更好...

关于objective-c - 我如何解决 iphone 中 imageWithContentsoffile 函数的内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13195877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com