有什么方法可以从我的“文档”中获取缓存的 UIImage目录?
我知道 -imageNamed: 方法返回一个缓存的 UIImage,但问题是我的图像文件存储在“文档”中,而 -imageNamed: 方法似乎只搜索 Bundle...我目前(不情愿地)使用-imageWithContentsOfFile:从“文档”获取我的图像,但它不一样......放大/缩小包含结果图像的 UIImageView 是不稳定和尴尬的。然而,缩放包含使用 -imageNamed: 创建的图像的同一 UIImageView 看起来非常顺利。所以,再说一遍:如果我无法使用 -imageNamed:,如何从我的“文档”中获取缓存的 UIImage:?
I know that the -imageNamed: method returns a Cached UIImage, but the problem is that my image file is stored in 'Documents', and the -imageNamed: method seems to only search the Bundle... I am currently (reluctantly) using -imageWithContentsOfFile: to get my image from 'Documents' but it is not the same...Scaling up/down a UIImageView containing the resulting image is choppy and awkward. Scaling the same UIImageView containing an image created with -imageNamed: however appears very smooth. So, again: How can I get a cached UIImage from my 'Documents' if I cannot use -imageNamed:?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对 rpetrich 提供的答案进行了扩展,并覆盖了 imageName: 方法以添加更多的替换内容。它首先搜索主包,然后查找缓存目录。您当然可以将缓存目录更改为文档目录。
I made an extension of the answer provided by rpetrich and overrode the imageName: method to add more of a drop in replacement. It searches the main bundle first and then looks in the caches directory. You could of course change the caches directory to the document directory.
最简单的方法是使用
NSMutableDictionary
存储缓存的图像和清除缓存方法:注意:您应该从
didReceiveMemoryWarning
调用+[UIImage clearCache]
> 方法。此外,clearCache
将使缓存中的所有对象无效,而不仅仅是未使用的项目;需要一个UIImage
子类和更复杂的缓存机制来解决这个问题。The simplest way would be an
NSMutableDictionary
storing the cached images and a clear cache method:Note: you should call
+[UIImage clearCache]
from yourdidReceiveMemoryWarning
method. Also,clearCache
will invalidate all objects in the cache, not just unused items; aUIImage
subclass and more complicated caching mechanism would be required to remedy this.您可以像
-imageNamed:
一样自行缓存UIImages
。它只是加载它们,然后保留它们。您也可以使用NSDictionary
保留它们并实现您自己的-imageNamed:
但我更担心您在缩放方面遇到的麻烦。您的图像如何进入文档,如何缩放它们,以及您是否测试了捆绑包中存储的相同图像文件?我怀疑
-imageNamed:
与此有任何关系。我更怀疑诸如捆绑应用了一些压缩的事实(尽管我还没有关于为什么这在实践中很重要的理论)、文件中的差异或程序其余部分的差异在扩展期间发生行为(导致磁盘或 CPU 争用)。缓存不太可能与此问题相关。我会使用仪器进行一些分析,试图找出不稳定的原因。您是否已最大化磁盘、CPU、内存?瓶颈是什么?
You can cache
UIImages
yourself just as-imageNamed:
does. It just loads them, and then holds onto them. You can hold onto them, too, using anNSDictionary
and implement your own-imageNamed:
But I'm more concerned about the trouble you're having with scaling. How are your images getting into Documents, how are you scaling them, and have you tested the same image file stored in the bundle? I doubt that
-imageNamed:
has anything to do with this. I would more suspect things like the fact that the bundle has some compression applied to it (though I don't yet have a theory on why this would matter in practice), differences in the file, or differences in how the rest of the program is behaving during scaling (causing contention on the disk or CPU). Caching is unlikely related to this issue.I'd do some profiling w/ Instruments to try to find out where the choppiness is coming from. Are you maxing out the disk, CPU, memory? What's the bottleneck?
编写自己的图像缓存怎么样?您已准备好所有部件,现在您只需封装它并保留已加载图像的记录即可。
What about writing your own image cache? You have all the pieces in place, now you just need to encapsulate it and keep a record of images you've already loaded.