将Apple Emoji(String)转换为UIImage [英] Convert Apple Emoji (String) to UIImage

查看:435
本文介绍了将Apple Emoji(String)转换为UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要所有Apple Emojis。
我可以通过从网站上复制它们来获取所有表情符号并将它们放入 String



示例



给出指示表情符号的unicode值的范围列表

  let ranges = [0x1F601 ... 0x1F64F,0x2702 ... 0x27B0] 

您可以将其转换为图像列表

 让图片=范围
。 flatMap {$ 0}
.compactMap {Unicode.Scalar($ 0)}
.map(Character.init)
.compactMap {String($ 0).image()}

结果:




我无法保证范围列表已完成,您需要搜索它b你自己

I need all Apple Emojis.
I can get all the emojis and put them into a String by copying them from the site getemoji but in my app i need the emojis in the right order as images.

Is there a nice way to convert the emojis I copy into a String to a UIImage?
Or a better solution to get all the Apple emojis in the right order?

解决方案

Updated for Swift 4.1

Add this extension to your project

import UIKit

extension String {
    func image() -> UIImage? {
        let size = CGSize(width: 40, height: 40)
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        UIColor.white.set()
        let rect = CGRect(origin: .zero, size: size)
        UIRectFill(CGRect(origin: .zero, size: size))
        (self as AnyObject).draw(in: rect, withAttributes: [.font: UIFont.systemFont(ofSize: 40)])
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

The code above draws the current String to an Image Context with a white background color and finally transform it into a UIImage.

Now you can write

Example

Given a list of ranges indicating the unicode values of the emoji symbols

let ranges = [0x1F601...0x1F64F, 0x2702...0x27B0]

you can transform it into a list of images

let images = ranges
    .flatMap { $0 }
    .compactMap { Unicode.Scalar($0) }
    .map(Character.init)
    .compactMap { String($0).image() }

Result:

I cannot guarantee the list of ranges is complete, you'll need to search for it by yourself

这篇关于将Apple Emoji(String)转换为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆