【发布时间】:2015-09-18 00:04:06
【问题描述】:
我正在为 iPhone 开发键盘扩展。有一个类似于 Apple 自己的表情符号键盘的表情符号屏幕,在 UICollectionView 中显示大约 800 个表情符号字符。
当滚动此表情符号UIScrollView 时,内存使用量会增加并且不会下降。我正确地重用了单元格,并且在使用显示 800 次的单个表情符号字符进行测试时,滚动期间内存不会增加。
使用工具,我发现我的代码中没有内存泄漏,但表情符号字形似乎已被缓存,并且可能需要大约 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 后,我得到如下内存使用图:
【问题讨论】:
标签: ios caching memory-management fonts emoji