I calculate a new color and need to render it in a gradient in the drawRect method of a view.
This is taking up a lot of execution time in time profiler and also leads to drawRect calls to be skipped or missed.
What would be the best way to optimise the rendering of a gradient in drawRect?
I have the following code
CGContextSaveGState(ctx);
CGContextAddPath(ctx, path.CGPath);
CGContextClip(ctx);
// get the color components
const CGFloat* components = CGColorGetComponents(fillColor.CGColor);
// create the gradient
CGGradientRef gradient;
CGColorSpaceRef colorspace;
size_t num_locations = 3;
CGFloat componentsArray[12] =
{
components[0], components[1], components[2], 1.0, // Start color
components[0], components[1], components[2], 1.0, // Middle color
components[0], components[1], components[2], 0.0 // End color
};
colorspace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(colorspace, componentsArray, locations, num_locations);
CGContextDrawLinearGradient (ctx, gradient, gradientStartPoint, gradientEndPoint, 0);
CGGradientRelease(gradient);
CGContextRestoreGState(ctx);