我知道在stackoverflow
and上有很多这样的问题,也可以在这个链接上找到答案: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
来做到这一点?
UIKit已经在使用图形加速。您的问题可能是在主线程上运行此方法。
我不知道你的应用程序在没有任何其他上下文的情况下应该做什么,但是你应该将这个方法的执行移动到后台线程。
如果您需要以这种方式处理大量图像,请考虑使用NSOperationQueue。看看那里:https://developer.apple.com/reference/foundation/operationqueue