CLOSE SEARCH

[Snippet] Draw Gradient using Quartz

– (void)drawGradient:(CGContextRef)context
{

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
1.0, 1.0, 1.0, 1.0,
0.5, 0.5, 0.5, 1.0,
0.0, 0.0, 0.0, 1.0
};

CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors) / (sizeof(colors[0]) * 4));
CGColorSpaceRelease(rgb);

CGRect rect = CGRectMake(20.0, 10.0, 50.0, 200.0);
CGContextClipToRect(context, rect);
CGContextDrawLinearGradient(context, gradient, CGPointMake(40, 10), CGPointMake(40, 210), 0);

CGContextSetLineWidth(context, 4.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, CGContextGetClipBoundingBox(context));

}

Filed under: iOS