Skip to content

Instantly share code, notes, and snippets.

@indragiek
Created May 3, 2011 03:30
Show Gist options
  • Save indragiek/952770 to your computer and use it in GitHub Desktop.
Save indragiek/952770 to your computer and use it in GitHub Desktop.
Replicating Photoshop Layer Styles Using Core Graphics
CGContextRef ctx = UIGraphicsGetCurrentContext(); // your drawing context
CGRect colorRect = CGRectMake(0, 0, 100, 100); // the rect to draw the color overlay into
// Set the blend mode to "Normal"
// This is the default blend mode setting so this line is not required, shown here for demonstration
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
// Set the opacity of the drawing context (once again, not required)
CGContextSetAlpha(ctx, 1.0);
// Create the red color
CGColorRef redColor = CGColorCreateGenericRGB(0.908, 0.149, 0.145, 1.000);
// Set the fill color of the context and fill the rect
CGContextSetFillColorWithColor(ctx, redColor);
CGContextFillRect(ctx, colorRect);
// Release allocated variables
CGColorRelease(redColor);
CGContextRef ctx = UIGraphicsGetCurrentContext(); // your drawing context
CGPoint startPoint = CGPointMake(0, 0); // the start point of the gradient in the view coordinate system
CGPoint endPoint = CGPointMake(0, 100); // the end point of the gradient in the view coordinate system
// Set the blend mode to "Screen"
CGContextSetBlendMode(ctx, kCGBlendModeScreen);
// Set the opacity of the graphics context
// The opacity is already set to 1.0 (100%) by default, it is shown here for demonstration purposes
CGContextSetAlpha(ctx, 1.00);
// Since we're using the RGB color model, create an RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create the colors (Core Graphics accepts color values in RGBA format)
CGColorRef redColor = CGColorCreateGenericRGB(0.806, 0.132, 0.129, 1.000);
CGColorRef greenColor = CGColorCreateGenericRGB(0.116, 0.308, 0.039, 1.000);
NSArray *colors = [NSArray arrayWithObjects:(id)redColor, (id)greenColor, nil];
// Set the locations of the color stops for the gradient (0.0 being the start, 1.0 being the end)
CGFloat locations[2] = {0.0, 1.0};
// Create the gradient and draw it!
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
// Release the variables that were allocated into memory
CGColorSpaceRelease(colorSpace);
CGColorRelease(redColor);
CGColorRelease(greenColor);
CGGradientRelease(gradient);
// Callback that is called to draw the pattern image
static void drawPatternImage (void *info, CGContextRef ctx)
{
CGImageRef image = (CGImageRef)info;
CGContextDrawImage(ctx, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
}
// Callback that is called after the pattern has been drawn
static void releasePatternImage(void *info)
{
CGImageRelease((CGImageRef)info);
}
CGContextRef ctx = UIGraphicsGetCurrentContext(); // your drawing context
CGRect patternRect = CGRectMake(0, 0, 100, 100); // the rect to draw the pattern into
// Set the blend mode to "Normal" (not required)
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
// Set the opacity of the drawing context (not required)
CGContextSetAlpha(ctx, 1.0);
// Create an image source to retrieve the image data from the image file
CFURLRef imagePath = (CFURLRef)[NSURL fileURLWithPath:@"/path/to/pattern/image.jpg"];
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imagePath, NULL);
// Create a CGImageRef object from the image data
CGImageRef patternImage = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
// Create a CGPatternRef from the image
// This part is somewhat complicated, so refer to the documentation for CGPattern and CGAffineTransform if necessary
CGFloat imageWidth = CGImageGetWidth(patternImage);
CGFloat imageHeight = CGImageGetHeight(patternImage);
static const CGPatternCallbacks callbacks = {0, &drawPatternImage, &releasePatternImage};
CGPatternRef pattern = CGPatternCreate(patternImage, CGRectMake(0, 0, imageWidth, imageHeight), CGAffineTransformIdentity, imageWidth, imageHeight, kCGPatternTilingConstantSpacing, true, &callbacks);
// Create a color space for the pattern
CGColorSpaceRef colorSpace = CGColorSpaceCreatePattern(NULL);
// Create a color from the pattern
CGFloat components[1] = {1.0};
CGColorRef patternColor = CGColorCreateWithPattern(colorSpace, pattern, components);
// Set the color as the fill color of the context and draw the pattern!
CGContextSetFillColorWithColor(ctx, patternColor);
CGContextFillRect(ctx, patternRect);
// Release allocated variables
CFRelease(imageSource);
CGPatternRelease(pattern);
CGColorSpaceRelease(colorSpace);
CGColorRelease(patternColor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment