如何从表情符号字符串

时间:2018-02-15 05:59:11

标签: swift uiimage

我正在尝试使用表情符号并将其转换为160x160高分辨率UIImage,但似乎我从我的方法中获得了一张颗粒状图片。

 func toImage(withDimension dimension: CGFloat) -> UIImage {
    let font = UIFont.systemFont(ofSize: dimension)

    let stringBounds = (self as NSString).size(withAttributes: [NSAttributedStringKey.font: font])
    UIGraphicsBeginImageContextWithOptions(stringBounds, false, 3.0)

    let xCenter = (stringBounds.width - CGFloat(dimension))/3.0
    let rect = CGRect(origin: CGPoint(x: xCenter, y: 0.0), size: stringBounds)
    UIColor.clear.set()
    UIRectFill(CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: stringBounds))

    (self as NSString).draw(in: rect, withAttributes: [NSAttributedStringKey.font: font])
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image!
 }

调用:

"".toImage(withDimension: 160.0)

enter image description here

有一个类似的线程,但似乎问题没有解决。是否可以从表情符号生成高分辨率的160x160图像?我正在使用Aspect Fill内容模式

0 个答案:

没有答案