合成版面标头内存泄漏

问题描述

我似乎遇到了内存泄漏,其布局布局使我的组大小导致我的标头和单元格经历了很高的内存使用率。为了更详细地说明,我尝试布局水平网格,其中项目根据屏幕的宽度具有1:1的比例。

在我的情况下,我希望每行5个项目,它们是屏幕尺寸的20%,每个项目之间的间距为1px。并且应该估计标题的高度,以便根据其内容自动调整大小。因此,下面是我到目前为止的内容摘要

// MARK: Sizing Constants
private let itemRatio: CGFloat = 0.2

var layout: UICollectionViewCompositionalLayout {
    
    UICollectionViewCompositionalLayout { [weak self] (sectionIndex,_) -> NSCollectionLayoutSection? in
        
        guard let self = self else { return nil }
                    
        let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(self.itemRatio),heightDimension: .fractionalWidth(self.itemRatio)))
        item.contentInsets.trailing = 1
        item.contentInsets.bottom = 1

        let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1),heightDimension: .fractionalWidth(self.itemRatio)),subitems: [item])
        let section = NSCollectionLayoutSection(group: group)
        
        // Enable this when finish working on the cells
        section.boundarySupplementaryItems = [
            .init(layoutSize: .init(widthDimension: .fractionalWidth(1),heightDimension: .estimated(38)),// Investigate why this is causing a memory leak...
                  elementKind: "CollectionViewHeader",alignment: .topLeading)
        ]

        return section
    }
}

如果我将下面的代码更改为下面的代码,即我为section标头定义了一个绝对值,那么一切都会很好,并且没有问题。

    // MARK: Sizing Constants
    private let itemRatio: CGFloat = 0.2
    
    var layout: UICollectionViewCompositionalLayout {
        
        UICollectionViewCompositionalLayout { [weak self] (sectionIndex,_) -> NSCollectionLayoutSection? in
            
            guard let self = self else { return nil }
                        
            let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(self.itemRatio),heightDimension: .fractionalWidth(self.itemRatio)))
            item.contentInsets.trailing = 1
            item.contentInsets.bottom = 1

            let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1),subitems: [item])
            let section = NSCollectionLayoutSection(group: group)
            
            // Enable this when finish working on the cells
            section.boundarySupplementaryItems = [
                .init(layoutSize: .init(widthDimension: .fractionalWidth(1),//                                        heightDimension: .estimated(38)),// Investigate why this is causing a memory leak...
                                        heightDimension: .absolute(sectionIndex < 1 ? 120 : 72)),// Investigate why this is causing a memory leak...
                      elementKind: "CollectionViewHeader",alignment: .topLeading)
            ]

            return section
        }
    }

有什么想法可能是这种情况吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)