How to clear font cache filled with emoji characters?

前端 未结 6 410
日久生厌
日久生厌 2020-12-13 19:52

I am developing keyboard extension for iPhone. There is an emoji screen smilar to Apples own emoji keyboard that shows some 800 emoji characters in UICollectionView

6条回答
  •  时光取名叫无心
    2020-12-13 20:20

    I ran into the same issue and fixed it by dumping the .png from /System/Library/Fonts/Apple Color Emoji.ttf and using UIImage(contentsOfFile: String) instead of a String.

    I used https://github.com/github/gemoji to extract the .png files, renamed the files with @3x suffix.

    func emojiToHex(emoji: String) -> String {
        let data = emoji.dataUsingEncoding(NSUTF32LittleEndianStringEncoding)
        var unicode: UInt32 = 0
        data!.getBytes(&unicode, length:sizeof(UInt32))
        return NSString(format: "%x", unicode) as! String
    }
    
    let path = NSBundle.mainBundle().pathForResource(emojiToHex(char) + "@3x", ofType: "png")
    UIImage(contentsOfFile: path!)
    

    UIImage(contentsOfFile: path!) is properly released so the memory should stay at a low level. So far my keyboard extension hasn't crashed yet.

    If the UIScrollView contains a lot of emoji, consider using UICollectionView that retains only 3 or 4 pages in cache and releases the other unseen pages.

提交回复
热议问题