Skip to content

Instantly share code, notes, and snippets.

@dlo
dlo / gradient.m
Created January 2, 2014 01:10
Draw a linear gradient in Objective-C
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = @[(__bridge id)HEX(0xC9C9CEFF).CGColor, (__bridge id)[UIColor whiteColor].CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
UIGraphicsBeginImageContext(CGSizeMake(1, 32));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, 32), 0);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();