简体   繁体   English

从 Bundle.main 加载/清除数据到 UICollectionView 的理想方法是什么?

[英]What's an ideal way to load/clear data to UICollectionView from Bundle.main?

I have about 100 GIFs in the main bundle.我的主包中有大约 100 个 GIF。 I'm loading everything during viewDidLoad() in an array.我在 viewDidLoad() 期间将所有内容加载到数组中。 This array is the datasource for the UICollectionView.这个数组是 UICollectionView 的数据源。 The problem is that these GIFs are taking a lot of memory which is causing slowness and lead to app crash due to memory.问题是这些 GIF 占用了大量内存,这会导致运行缓慢并导致应用程序因内存而崩溃。 When I start scrolling, the memory debugger shows up to 800 mb+ and then crashes.当我开始滚动时,内存调试器显示高达 800 mb+,然后崩溃。

碰撞

I considered integrating 3rd-party libraries to optimize the GIFs performances.我考虑集成第 3 方库来优化 GIF 性能。 Then I thought about creating some sort of local caching solution where I can offload memory and fetch data when needed in the background once the cell goes offview.然后我考虑创建某种本地缓存解决方案,一旦单元格离开视图,我可以在后台卸载内存并在需要时获取数据。 Is this a right approach or am I overcomplicating things?这是一种正确的方法还是我把事情复杂化了?

  1. Load gif bundle urls (only url, not image data) in viewDidLoad .viewDidLoad加载 gif 包 url(仅 url,而不是图像数据)。
override func viewDidLoad() {
    gifUrls = ...
}
  1. Use 3rd-party view in collection view cell to display gif.在集合视图单元格中使用第 3 方视图来显示 gif。 For example, we use BBWebImage .例如,我们使用BBWebImage
// In collection view cell
imageView = BBAnimatedImageView(frame: frame)
  1. In load gif asynchronously in collectionView(_:cellForItemAt:) .collectionView(_:cellForItemAt:)异步加载 gif。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellReuseID, for: indexPath)
    cell.tag = indexPath.item
    let url = gifUrls[indexPath.item]
    DispatchQueue.global().async {
        guard let data = try? Data(contentsOf: url) else { return }
        let gif = BBAnimatedImage(bb_data: data)
        DispatchQueue.main.async {
            guard cell.tag == indexPath.item else { return }
            cell.imageView.image = gif
        }
    }
}

As @rmaddy said you could load gifs in cycle of visible cells by these methods:正如@rmaddy 所说,您可以通过以下方法在可见单元格的循环中加载 gif:

func collectionView(_ collectionView: UICollectionView, 
             willDisplay cell: UICollectionViewCell, 
               forItemAt indexPath: IndexPath)

More details 更多细节

And:和:

func collectionView(_ collectionView: UICollectionView, 
        didEndDisplaying cell: UICollectionViewCell, 
               forItemAt indexPath: IndexPath)\

More details 更多细节

Load gif on willDisplay and offload it on didEndDisplaying .负载GIF willDisplay和卸载它didEndDisplaying

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

相关问题 从 XIB 创建自定义视图时,我们应该更喜欢使用 Bundle.main 还是 UINib? - Should we prefer to use Bundle.main or UINib when creating custom view from XIB? 从核心数据加载UICollectionView - load UICollectionView from core data 将来自Core Data的调整大小的图像插入UICollectionView的最简单方法是什么? - What is the simplest way to insert resized images from Core Data into a UICollectionView? 我可以在ViewDidLoad中加载UICollectionView的数据吗? - Can I load a UICollectionView's data in ViewDidLoad? 在UICollectionView中重新加载数据的正确方法是什么? - What is the correct way to reload data in a UICollectionView? 在 Core Data 中使用多线程的理想方法是什么? - What is ideal way to use multi threading with Core Data? 使用Monotouch和iOS从服务中提取数据(JSON)的理想方式? - Ideal way to pull data (JSON) from a service using Monotouch and iOS? 在iOS中从内存中清除敏感数据的正确方法是什么? - What is the correct way to clear sensitive data from memory in iOS? 在将UICollectionView的数据添加到屏幕之前,先对其进行加载 - Load A UICollectionView's Data Before It's Added To The Screen 在UICollectionView中构建非常灵活/动态的“标题”的最佳方法是什么? - What's the best way to build a very flexible/dynamic “header” in UICollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM