Skip to content

Instantly share code, notes, and snippets.

@shawnwall
Created April 19, 2012 19:37
Show Gist options
  • Save shawnwall/2423595 to your computer and use it in GitHub Desktop.
Save shawnwall/2423595 to your computer and use it in GitHub Desktop.
UIViewController gradient border
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 0.0, // Start color
1.0, 1.0, 1.0, 1.0 }; // End color
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGColorSpaceRelease(rgbColorspace);
// Allocate color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Allocate bitmap context
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, 320, 480, 8, 4 * 320, colorSpace, kCGImageAlphaNoneSkipFirst);
// Draw Gradient Here
CGContextDrawLinearGradient(bitmapContext, gradient, CGPointMake(0.0f, 0.0f), CGPointMake(0.0f, 480.0f),0 );
CGGradientRelease(gradient);
// Create a CGImage from context
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
// Create a UIImage from CGImage
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
// Release the CGImage
CGImageRelease(cgImage);
// Release the bitmap context
CGContextRelease(bitmapContext);
// Release the color space
CGColorSpaceRelease(colorSpace);
// Create the patterned UIColor and set as background color
self.view.layer.borderColor = [UIColor colorWithPatternImage:uiImage].CGColor;
self.view.layer.borderWidth = 2.0f;
}
return self;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment