我目前的任务是iOS键盘扩展,其中包括所有iOS支持的表情符号(是的,我知道iOS有一个内置表情符号键盘,但目标是在键盘扩展中包含一个).
对于这个基本上应该是以网格顺序包含所有表情符号的滚动视图的表情符号布局,我决定使用UICollectionView,因为它只创建有限数量的单元格并重用它们.(有很多表情符号,超过1000个.)这些单元格只包含一个UILabel,它将表情符号作为文本,并使用GestureRecognizer插入抽头表情符号.
但是,当我滚动列表时,我可以看到内存使用量爆炸大约16-18MB到33MB以上.虽然这不会在我的iPhone 5s上触发内存警告,但它也可能在其他设备上,因为应用程序扩展仅专用于非常少量的资源.
编辑:有时我收到内存警告,主要是切换回"普通"键盘布局.大多数情况下,切换回来时内存使用量会降至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 …Run Code Online (Sandbox Code Playgroud)