Draw a multicolor line with CGPath

below you see code that draws a multicolor line every time drawMulticolorLine is called.

void drawMulticolorLine { CGContextBeginPath(secondaryContext); CGContextMoveToPoint(secondaryContext, h, v); for( int i = 1; i < lineWidth; ++ ) { SetStrokeColor(i); CGContextAddLineToPoint(secondaryContext, h+i, v); CGContextDrawPath(secondaryContext, kCGPathFillStroke); CGContextMoveToPoint(secondaryContext, h+i, v); } } //a function that sets a different color for each i void SetStrokeColor(int i) { CGContextSetRGBStrokeColor(secondaryContext,… } 

the above code works, but if I use it in real time, it is a performance killer. so I'm trying to improve drawing performance with the code below.

  void drawMulticolorLine { CGContextBeginPath(secondaryContext); CGContextMoveToPoint(secondaryContext, h, v); for( int i = 1; i < lineWidth; ++ ) { SetStrokeColor(i); CGContextAddLineToPoint(secondaryContext, h+i, v); } CGContextDrawPath(secondaryContext, kCGPathFillStroke); } //a function that sets a different color for each i void SetStrokeColor(int i) { CGContextSetRGBStrokeColor(secondaryContext,… } 

this code works with good performance if the line color is always the same.

Now my problem is that after calling CGContextDrawPath the whole line is drawn in the color of the last CGContextSetRGBStrokeColor.

Is there a way that the path preserves the colors of each line segment that has been added?

any help is appreciated.

amuses, bzt

+4
source share

All Articles