【问题标题】:iMessage application - Memory leak when display GIF as UICollectionViewCell?iMessage 应用程序 - 将 GIF 显示为 UICollectionViewCell 时内存泄漏?
【发布时间】:2017-04-27 04:09:06
【问题描述】:

大家好,我的记忆有问题。我尝试使用Kingfisher 将 GIF 图像显示为 UICollectionViewCell。每次我回到以前的 ViewController 并回到当前的 ViewController 时,内存都会异常增长(加倍)。我正在使用带有一些相关库的 RxSwift。

这是调用 giphy api 源,并绑定到集合视图:

provider = RxMoyaProvider<Giphy>()
    giphyTrackerModel = GiphyTrackerModel(provider: provider, gifUrl: latestGiphyName)
    giphyTrackerModel
        .trackGiphy()
        .bindTo(collectionView.rx.items(cellIdentifier: "SearchCell", cellType: SearchCell.self)) {
            (index, giphyDataModel, cell) in
            cell.delegate = self
            cell.giphy = giphyDataModel
        }
        .addDisposableTo(disposeBag)

这是我的 SearchCell.swift:

import UIKit
import Kingfisher    
class SearchCell: UICollectionViewCell {

    @IBOutlet weak var gifImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()

        gifImageView.layer.cornerRadius = 4
        gifImageView.layer.masksToBounds = true
        self.backgroundColor = Color.lightGray
        self.layer.cornerRadius = 4
    }

    weak var giphy: GiphyDataModel! { 
        didSet {
            if let gifUrl = giphy.images?.downsized?.url { 
                 let url = gifUrl.characters.count != 0 ? URL(string: gifUrl) : URL(string: "")
                 gifImageView.kf.setImage(with: url)
             }
        }
    }
}

【问题讨论】:

  • 您可能想检查您的单元格的委托是否是对自我的强引用。这可能会导致强引用循环而不是内存泄漏。虽然更好的方法是使用仪器来找出。
  • 感谢@BenOng,我已尝试删除委托。内存泄漏仍然存在。可怜的我:(
  • 另一个人得到了相同的issue,但仍然没有详细的解决方案。
  • 好吧,根据您的展示,我也无法提供详细的解决方案。如果您没有使用仪器找到泄漏的位置,您应该先这样做。确定导致内存没有被释放的原因是修复它的第一步。

标签: ios swift memory-leaks gif imessage


【解决方案1】:

尝试使用 MSStickerView 代替 UIImageView。它应该会减少一点内存使用。缓存文件也可能有问题。你应该检查一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-20
    • 2016-03-28
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多