简体   繁体   English

swift - UICollectionView或UIImage - 如何防止图像缓存

[英]swift - UICollectionView or UIImage - how to prevent image caching

I have a UICollectionView with 4 cells in a grid pattern and no scrolling . 我有一个UICollectionView ,网格模式中有4个cells格,没有scrolling Each cell has a single imageView . 每个单元格都有一个imageView The images are stored in the asset folder and retrieved by name reference (see below). images存储在资产文件夹中,并通过名称参考检索(见下文)。 After some UI action, the idea is to 在一些UI动作之后,我们的想法是

collectionView.reloadData() 

and replace the image in each cell . 并替换每个cellimage

Everything works fine, but the memory use keeps climbing after each reload. 一切正常,但每次重装后记忆的使用都会不断climbing I had assumed reloadData() dumps unnecessary data , but this experience has me puzzled. 我曾假设reloadData()转储unnecessary data ,但这种经历让我感到困惑。 What's happening? 发生了什么?

If it helps, I read somewhere loading an image with 如果它有帮助,我会在某处加载图像

UIImage(name: ...) 

caches the image. 缓存图像。 I've tried to confirm this but have drawn a blank. 我试图证实这一点,但已经画了一个空白。

My code is pretty straightforward so I'm wondering if this is some memory bug / feature for scrolling purposes (that I don't want). 我的代码非常简单,所以我想知道这是否是一些内存错误/功能用于滚动目的(我不想要)。

Once again, I'm looking to stop the memory stockpiling. 再一次,我想停止内存储存。 Any ideas? 有任何想法吗?

I never had the chance to understand when the image cache is evicted. 我从未有机会了解何时驱逐图像缓存。
Collection view cell are dequeued and reused. 集合视图单元格已出列并重复使用。 That means than right after a cell goes out of screen is put in a recycle bin, if the cell is need again is taken from that recycle bin and placed into you views hierarchy (this is called flyweight pattern I guess), basically you are saving the instantiation cost. 这意味着,当一个单元格离开屏幕后,将其放入回收站,如果再次需要单元格从该回收站中取出并放入您的视图层次结构(我猜这称为flyweight模式),基本上您正在保存实例化成本。
Thus when you deal with images and your images are not stored elsewhere in memory but just inside the image view, every time a cell is shown and an new image is passed to the image view the old image should be freed. 因此,当您处理图像并且图像未存储在存储器中的其他位置而仅存储在图像视图内部时,每次显示单元格并将新图像传递到图像视图时,应释放旧图像。
This could not happen if you store a reference to the old image elsewhere, for instance a cache or an array. 如果您将旧映像的引用存储在其他位置(例如缓存或数组),则不会发生这种情况。
To load image from xcasset you can only use UIImage(name: ...) that means the image will be cached. 要从xcasset加载图像,您只能使用UIImage(name: ...) ,这意味着图像将被缓存。 Even if Apple says that this cache will be evicted in memory pressure situations and never seen this happening "in time" to avoid the app crashing. 即使Apple说这个缓存会在内存压力情况下被驱逐,并且从来没有“及时”发生这种情况以避免应用程序崩溃。
What you can do is do not save in the xcasset folder, but anywhere in your project, in this way they will be loaded into your main bundle, you can load images from here by using the common: 您可以做的是不要保存在xcasset文件夹中,而是保存在项目的任何位置,这样它们将被加载到您的主包中,您可以使用以下方法从此处加载图像:

let path = NSBundle.mainBundle().pathForResource(<#T##name: String?##String?#>, ofType: <#T##String?#>)
    let image = UIImage(contentsOfFile: path!)

Pay attention that path is an optional. 注意路径是可选的。
As a general rule, always try to save resources by using image of the same view size in pixel, the occupied space of an image means nothing, just how its compressed, when loaded in memory it takes n_pixel_height * n_pixel_width * n_channels bytes 作为一般规则,总是尝试通过使用像素中相同视图大小的图像来节省资源,图像的占用空间意味着什么,只是如何压缩,当在内存中加载它需要n_pixel_height * n_pixel_width * n_channels bytes

You are doing it correctly, If you are using dequeueReusableCellWithReuseIdentifier(identifier:, forIndexPath:) method for cell reusability then it will surely handle the reusability of the cell. 您正在正确地执行此操作,如果您正在使用dequeueReusableCellWithReuseIdentifier(identifier:, forIndexPath:)方法来获取单元可重用性,那么它肯定会处理单元的可重用性。

You have no other way to load the images from the bundle. 您没有其他方法可以从捆绑中加载图像。

Some slight changes in memory may reflect but its ok and go ahead 记忆中的一些细微变化可能会反映出来但是还可以继续

In prepareForReuse method of your subclass UICollectionViewCell 在您的子类UICollectionViewCell的prepareForReuse方法中

func prepareForReuse()
{
    super.prepareForReuse();
    cell.imageView.image = nil;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM