ios – UICollectionView中的高内存使用量

参见英文答案 > How to clear font cache filled with emoji characters?4个
我目前的任务是iOS键盘扩展,其中提供了所有iOS支持的表情符号(是的,我知道iOS有一个内置的表情符号键盘,但目标是在键盘扩展中包含一个).

对于这个Emoji布局,它基本上应该是一个滚动视图,所有的emojis在它的网格顺序,我决定使用一个UICollectionView,因为它只创建有限数量的单元格并重新使用它们. (有相当多的emojis,超过1’000).这些单元格简单地包含一个UILabel,它将表情符号作为其文本,用GestureRecognizer来插入轻敲的表情符号.

然而,当我浏览列表时,我可以看到内存使用量爆炸了大约16-18MB到超过33MB.虽然这并不会引发iPhone 5上的内存警告,但是在其他设备上也可能会出现内存警告,因为应用扩展只是专用于非常少量的资源.

编辑:有时我会收到内存警告,主要是切换回“普通”键盘布局时.大多数情况下,当切换回来时,内存使用量下降到20MB以下,但不总是.

如何减少此表情符布局使用的内存量?

class EmojiView: UICollectionViewCell {

    //...

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.userInteractionEnabled = true
        let l = UILabel(frame: self.contentView.frame)
        l.textAlignment = .Center
        self.contentView.addSubview(l)
        let tapper = UITapGestureRecognizer(target: self,action: "tap:")
        self.addGestureRecognizer(tapper)
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        //We kNow that there only is one subview of type UILabel
        (self.contentView.subviews[0] as! UILabel).text = nil
    }
}

//...

class EmojiViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    //...

    override func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //The reuse id "emojiCell" is registered in the view's init.
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("emojiCell",forIndexPath: indexPath)
        //Get recently used emojis
        if indexPath.section == 0 {
            (cell.contentView.subviews[0] as! UILabel).text = recent.keys[recent.startIndex.advancedBy(indexPath.item)]
        //Get emoji from full,hardcoded list
        } else if indexPath.section == 1 {
            (cell.contentView.subviews[0] as! UILabel).text = emojiList[indexPath.item]
        }
        return cell
    }

    //Two sections: recently used and complete list
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }

}

let emojiList: [String] = [
    "\u{1F600}","\u{1F601}","\u{1F602}",//...
    // I can't loop over a range,there are
    // unused values and gaps in between.
]

如果您需要更多的代码和/或信息,请通知我.

编辑:我的猜测是,尽管在重新使用前将文本设置为零,但是iOS会将呈现的emojis保留在内存中.但我可能完全错了

编辑:根据JasonNam的建议,我使用Xcode的Leaks工具运行键盘.在那里我注意到两件事情:

> VM:当滚动时,CoreAnimation最多可以达到6-7MB左右,但是我想这可能是滚动浏览集合视图时的正常现象.
> Malloc 16.00KB,以千字节为单位开始,在滚动整个列表时拍摄到17MB,所以有很多内存被分配,但我看不到其他实际使用的内存.

但没有报告泄漏.

EDIT2:我刚刚使用CfgetRetainCount(在使用ARC时仍然可以工作),一旦设置了prepareForReuse中的nil值,String对象就没有任何引用.

我在使用iOS 9.2的iPhone 5s上进行测试,但问题出现在使用iPhone 6s Plus的模拟器中.

EDIT3:有一个here完全相同的问题,但是由于这个奇怪的题目,到目前为止我还没有找到.看来,唯一的解决方案是在列表中使用UIImage的UIImageViews,因为UICollectionView中的UIImages在单元重用时已正确释放.

解决方法

在我的测试项目中,我非常有趣,我在EmojiView中评论了prepareForReuse部分,内存使用情况稳定,项目开始于19MB,从不超过21MB,(self.contentView.subviews [0] as!UILabel) .text = nil在我的测试中引起问题.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...