Skip to content

Instantly share code, notes, and snippets.

@adow
Created December 5, 2013 06:48
Show Gist options
  • Save adow/7801165 to your computer and use it in GitHub Desktop.
Save adow/7801165 to your computer and use it in GitHub Desktop.
Make a line graident image
-(UIImage*)makeGradientImage{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0f);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 0.0,0.0,0.0, 0.0, // Start color
0.0,0.0,0.0,1.0}; // End color
myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components,
locations, num_locations);
CGContextDrawLinearGradient(context, myGradient, CGPointMake(self.bounds.size.width/2, 100.0f), CGPointMake(self.bounds.size.width/2, self.bounds.size.height-100.0f), kCGGradientDrawsAfterEndLocation);
UIImage* image=UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
CGColorSpaceRelease(myColorspace);
CGGradientRelease(myGradient);
UIGraphicsEndImageContext();
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment