Skip to content

Instantly share code, notes, and snippets.

@dlo
Created January 2, 2014 01:10
Show Gist options
  • Save dlo/8213579 to your computer and use it in GitHub Desktop.
Save dlo/8213579 to your computer and use it in GitHub Desktop.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment