我正在为iPhone开发键盘扩展程序。有一个表情符号屏幕,类似于苹果自己的表情符号键盘,其中在UICollectionView
中显示了大约800个表情字符。
当这个表情UIScrollView
被滚动时,内存使用量会增加而不会下降。我正确地重用了单元格,并且在测试时,只显示单个表情字符800次时,在滚动期间内存不会增加。
使用Instruments,我发现我的代码中没有内存泄漏,但似乎表情符号被缓存并且根据字体大小可以占用大约10-30MB的内存(研究显示它们实际上是PNG文件)。键盘扩展在被杀死之前可以使用很少的内存。是否有一种方法可以清除该字体缓存?
编辑
添加了可以重现问题的代码示例:
let data = Array("☺️✨✊✌️✋☝️⭐️☀️⛅️☁️⚡️☔️❄️⛄️☕️❤️️⚽️⚾️⛳️").map {String($0)}
class CollectionViewTestController: UICollectionViewController {
override func viewDidLoad() {
collectionView?.registerClass(Cell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! Cell
if cell.label.superview == nil {
cell.label.frame = cell.contentView.bounds
cell.contentView.addSubview(cell.label)
cell.label.font = UIFont.systemFontOfSize(34)
}
cell.label.text = data[indexPath.item]
return cell
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
}
class Cell: UICollectionViewCell {
private let label = UILabel()
}
运行并滚动UICollectionView
后,我得到了这样的内存使用图表: