You need to enable JavaScript to run this app. UICollectionView加载Emoji使用UILabel时内存持续增长且无法回收的问题
优惠活动
大模型
产品
解决方案
定价
更多

UICollectionView加载Emoji使用UILabel时内存持续增长且无法回收的问题

问题描述

我尝试搭建如下样式的表情列表:
列表样式示例截图

以下是我编写的Cell代码:

class EmojiCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        emojiLabel.text = nil
    }

    lazy var emojiLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: "Apple color emoji", size: 30)
        view.textAlignment = .center
        return view
    }()

    private func makeUI() {
        contentView.addSubview(emojiLabel)
        emojiLabel.translatesAutoresizingMaskIntoConstraints = false
        emojiLabel.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        emojiLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        emojiLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        emojiLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    }

    func setEmoji(emoji: String) {
        emojiLabel.text = emoji
    }
}

遇到的问题

  • 滚动 UICollectionView 时,内存持续增长
    内存增长趋势截图
  • 关闭该页面后,已占用的内存没有被回收
  • 检测发现存在 CFString 类型的内存泄漏
    CFString内存泄漏检测截图

希望能得到这个问题的解决思路,非常感谢。


内容的提问来源于stack exchange,提问作者wanbo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 15:15:04