1

I know have many questions like this on stackoverflowand also have the answer for it like on this link: How to convert text to Image on iOS?. But with me, I need to draw many (more than 800 image) UIImages. So that, if I do my job as follow:

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
}

My app will run so very slowly ~ app get jerky. I hear about the GPU acceleration but don't have any experience on GPU. How to draw image (as my function) but use the GPU to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
lee
  • 7,955
  • 8
  • 44
  • 60

1 Answers1

0

UIKit is already using graphics acceleration. Your problem probably is running this method on the main thread.

I dont't know what your app should do without any other context, but you should move the execution of this method to a background thread.

If you need to process a big number of images this way, think about using NSOperationQueue. Have a look there: https://developer.apple.com/reference/foundation/operationqueue

davidv
  • 373
  • 5
  • 14
  • Any comment on why it got downvoted? I don't see any reason, it is legitimate advice. – davidv Oct 03 '16 at 07:59
  • You answer does not answer the question. The question is about sending the work to the GPU. – naz Jul 23 '17 at 02:00