Drawing performance UIGraphicsBeginImageContextWithOptions scale factor with Swift in Xcode for iOS app
Asked Answered
A

1

9

I have a method for drawing, but drawing performance is greatly affected when adjusting the scale factor for UIGraphicsBeginImageContextWithOptions.

When the scale is 1.0, everything works super fast, however, when the scale factor is either 0.0, 2.0 or 3.0 for retina displays, the performance is terrible with lots of lagging while drawing.

What can be modified to improve performance when using scale factor of 0.0, 2.0 or 3.0 for retina devices?

Draws slow, lagging:

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0.0)
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 2.0)
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 3.0)

Draws fast, no lagging:

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 1.0)
Avenge answered 25/9, 2015 at 15:57 Comment(5)
Keep in mind that using 2.0 requires 4 times the amount of pixels to be drawn and using 3.0 requires 9 times the amount of pixels. Using 0.0 is the same as 2.0 or 3.0 depending on the device.Stoss
Yes, that's true. Just wondering how performance can be optimised. The lagging equally occurs on an older iPhone (2011) and newer iPad (2014). I'd choose scale factor 1.0, except that I can see the pixels.Avenge
You could use Instruments and analyse the piece of code that does the actual drawing and see where the most time is spent trying to improve that after that. Not showing us the actual drawing code it's pretty hard to understand what's taking the extra timeEnswathe
post some code where you are using and how you are drawing, And you should use UIScreen.main.scale, so based on the device it decides which scale to use. I think it's like calculating to fit 3 dp in 2 dp space if you set 3.0 for 2x displayAerophyte
how small is the view? Maybe construct a larger version in the background and turn it into an image. My guess is this would be fasterNonobjective
E
5

I'm not sure what kind of drawing you are performing, as the advice would vary depending on the application. For example, if you're drawing Text, then there are numerous settings to be configured on the current context like context?.setShouldSmoothFonts, setShouldSubpixelQuantizeFonts or setShouldSubpixelPositionFonts.

Apple by default, sets the quality of drawn images to be very high, and hence you may need to optimise performance by tweaking parameters of the current Core Graphics context, as shown below.

  1. You can try (before drawing) setting the CGInterpolationQuality of the current CGContext. There is a significant rendering speed improvement for .none or .low compared to .medium and .high, but this has to be balanced with image quality. Try as follows:

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0.0)
    let context = UIGraphicsGetCurrentContext()
    context?.interpolationQuality = .medium //Change this value as per your needs
    //Insert drawing code here.
    
  2. Consider using Patterns to speed up drawing code. e.g UIColor(patternImage: image).

  3. Tweak context parameters such as context?.setMiterLimit(-10) and context?.setShouldAntialias(false).

Note that you can try tweaking these context parameters using the scale factor of 1.0 as well as 0.0 in the UIGraphicsBeginImageContextWithOptions depending on the use case. To provide even faster rendering, with a lower image quality a parameter of 1.0 would make sense, whereas for a better image quality the scale factor can be set to 0.0.

Eady answered 26/6, 2018 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.