【问题标题】:How to convert text to UIImage (use GPU)如何将文本转换为 UIImage(使用 GPU)
【发布时间】:2017-02-10 08:40:12
【问题描述】:

我知道在stackoverflow 上有很多这样的问题,并且在这个链接上也有答案:How to convert text to Image on iOS?。但对我来说,我需要绘制许多(超过 800 张图像)UIImages。因此,如果我按以下方式工作:

func createImage(text: String, size: CGSize) -> UIImage {

    let data = text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
    let drawText = NSString(data: data!, encoding: NSUTF8StringEncoding)

    let textFontAttributes = [
        NSFontAttributeName: UIFont(name: "Helvetica Bold", size: 15)!,
        NSForegroundColorAttributeName: UIColor.redColor(),
        ]

    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    drawText?.drawInRect(CGRectMake(0, 0, size.width, size.height), withAttributes: textFontAttributes)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

我的应用会运行得很慢 ~ 应用会生涩。我听说过GPU acceleration,但对GPU 没有任何经验。如何绘制图像(作为我的功能)但使用GPU 来执行此操作?

【问题讨论】:

    标签: ios objective-c swift uiimage cpu


    【解决方案1】:

    UIKit 已经在使用图形加速。您的问题可能是在主线程上运行此方法。

    我不知道你的应用在没有任何其他上下文的情况下应该做什么,但你应该将此方法的执行移至后台线程。

    如果您需要以这种方式处理大量图像,请考虑使用 NSOperationQueue。 看看那里:https://developer.apple.com/reference/foundation/operationqueue

    【讨论】:

    • 对它被否决的原因有何评论?我看不出任何理由,这是合理的建议。
    • 你回答不回答问题。问题是关于将工作发送到 GPU。
    猜你喜欢
    • 2023-04-09
    • 2011-07-25
    • 2015-06-13
    • 2011-03-10
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多