Andrew
Andrew

Reputation: 16051

CoreGraphics isn't drawing as expected when given gradient

This is my code:

CGContextSaveGState(context);
    CGContextAddRect(context, CGContextGetClipBoundingBox(context));
    CGContextAddPath(context, pickerPath);
    CGContextEOClip(context);
    CGContextAddPath(context, pickerPath);

//Gradient
    CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat colors[20];

    colors[0] = 0.255;
    colors[1] = 0.255;
    colors[2] = 0.286;
    colors[3] = 1.0;
    colors[4] = 0.169;
    colors[5] = 0.169;
    colors[6] = 0.188;
    colors[7] = 1.0;
    colors[8] = 0.153;
    colors[9] = 0.153;
    colors[10] = 0.173;
    colors[11] = 1.0;
    colors[12] = 0.133;
    colors[13] = 0.133;
    colors[14] = 0.149;
    colors[15] = 1.0;
    colors[16] = 0.122;
    colors[17] = 0.122;
    colors[18] = 0.137;
    colors[19] = 1.0;

    CGFloat locations[] = { 0.0, 0.171, 0.580, 0.580, 1.0 };

    CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, locations, 5);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, 257), 0);

//Red fill
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillPath(context);

    CGContextRestoreGState(context);

It draws the gradient, but isn't drawing the red fill. If i comment out the gradient, it still doesn't draw the red fill.

Any ideas why?

Upvotes: 0

Views: 128

Answers (1)

Vladimir
Vladimir

Reputation: 170839

The problem is probably that you're trying to fill an ellipse while context is still clipped. Try to make drawing in 2 operations:

CGContextSaveGState(context);
// Draw gradient with ellipse clipped-out as you do now
CGContextRestoreGState(context);
CGContextSaveGState(context);
// Set ellipse path again and fill it
CGContextRestoreGState(context);

Upvotes: 1

Related Questions