Created
January 4, 2013 01:47
-
-
Save syzdek/4449236 to your computer and use it in GitHub Desktop.
Generates a tile image from an input image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage *) tileImageFromImage:(UIImage *)image | |
{ | |
CGImageRef imageRef; | |
CGContextRef context; | |
CGColorSpaceRef color; | |
CGFloat w; | |
CGFloat h; | |
CGFloat r; | |
CGFloat m; | |
NSInteger pos; | |
CGFloat components[8]; | |
CGGradientRef gradLight; | |
CGGradientRef gradDark; | |
// image information | |
w = image.size.width; | |
h = image.size.height; | |
r = w * 0.1; | |
r = (r < 10) ? 10 : r; | |
m = 0; | |
NSLog(@"Image dimensions: %fx%f", w, h); | |
// create drawing context using UIImage parameters | |
color = CGColorSpaceCreateDeviceRGB(); | |
context = CGBitmapContextCreate( | |
NULL, // pointer where drawing is rendered | |
CGImageGetWidth(image.CGImage), // The width, in pixels, of the bitmap | |
CGImageGetHeight(image.CGImage), // The height, in pixels, of the bitmap | |
CGImageGetBitsPerComponent(image.CGImage), // bits to use for each component of a pixel | |
CGImageGetBytesPerRow(image.CGImage), // bytes of memory to use per row of the bitmap | |
CGImageGetColorSpace(image.CGImage), // The color space to use for the bitmap context | |
kCGImageAlphaPremultipliedLast // Specify whether the bitmap contains alpha | |
); | |
// First Pass: Round corners of context and import image | |
// Second Pass: trace outside border of bevel | |
// | |
// A B C D | |
// *-*-----------*-* A = (m, m) G = (w-m, h-m) | |
// L * * E B = (r+m, m) H = (w-r-m, h-m) | |
// | | C = (w-r-m, m) I = (r+m, h-m) | |
// | | D = (w-m, m) J = (m, h-m) | |
// | | E = (w-m, h-r-m) K = (m, h-r-m) | |
// | | F = (w-m, h-r-m) L = (m, r+m) | |
// K * * F | |
// *-*-----------*-* w = width r = bend radius | |
// J I H G h = height m = margin | |
// | |
for(pos = 0; pos < 2; pos++) | |
{ | |
CGContextMoveToPoint(context, r+m, m); // start: B | |
CGContextAddLineToPoint(context, r+m, m); // line: C | |
CGContextAddArcToPoint(context, w-m, m, w-m, h-r-m, r); // arc: D,E | |
CGContextAddLineToPoint(context, w-m, h-r-m); // line: F | |
CGContextAddArcToPoint(context, w-m, h-m, w-r-m, h-m, r); // arc: G,H | |
CGContextAddLineToPoint(context, r+m, h-m); // line: I | |
CGContextAddArcToPoint(context, m, h-m, m, h-r-m, r); // arc: J,K | |
CGContextAddLineToPoint(context, m, r+m); // line: L | |
CGContextAddArcToPoint(context, m, m, r+m, m, r); // arc: A,B | |
CGContextClosePath(context); | |
if (pos == 0) | |
{ | |
// clip of context | |
CGContextClip(context); | |
// copy image into context | |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), image.CGImage); | |
}; | |
}; | |
// trace inside border of bevel | |
m = (r/2); | |
r /= 2; | |
CGContextMoveToPoint(context, r+m, m); // start: B | |
CGContextAddArcToPoint(context, m, m, m, r+m, r); // arc: A,L | |
CGContextAddLineToPoint(context, m, h-r-m); // line: K | |
CGContextAddArcToPoint(context, m, h-m, r+m, h-m, r); // arc: J, I | |
CGContextAddLineToPoint(context, w-r-m, h-m); // line: H | |
CGContextAddArcToPoint(context, w-m, h-m, w-m, h-r-m, r); // arc: G, F | |
CGContextAddLineToPoint(context, w-m, h-r-m); // line: E | |
CGContextAddArcToPoint(context, w-m, m, w-r-m, m, r); // arc: D, C | |
CGContextAddLineToPoint(context, r+m, m); // line: B | |
CGContextClosePath(context); | |
CGContextClip(context); | |
// creates dark gradient | |
components[0] = 0.0; // first color (red) | |
components[1] = 0.0; // first color (green) | |
components[2] = 0.0; // first color (blue) | |
components[3] = 0.4; // first color (alpha) | |
components[4] = 0.0; // second color (red) | |
components[5] = 0.0; // second color (green) | |
components[6] = 0.0; // second color (blue) | |
components[7] = 0.0; // second color (alpha) | |
gradDark = CGGradientCreateWithColorComponents(color, components, NULL, 2); | |
// creates light gradient | |
components[0] = 1.0; // first color (red) | |
components[1] = 1.0; // first color (green) | |
components[2] = 1.0; // first color (blue) | |
components[3] = 0.4; // first color (alpha) | |
components[4] = 1.0; // second color (red) | |
components[5] = 1.0; // second color (green) | |
components[6] = 1.0; // second color (blue) | |
components[7] = 0.0; // second color (alpha) | |
gradLight = CGGradientCreateWithColorComponents(color, components, NULL, 2); | |
// applies gradient to bottom side | |
CGContextDrawLinearGradient | |
( | |
context, // A Quartz graphics context. | |
gradDark, // A CGGradient object. | |
CGPointMake(0, 0), // Starting point of the gradient. | |
CGPointMake(0, (m*2)), // Ending point of the gradient. | |
kCGGradientDrawsBeforeStartLocation | |
); | |
// applies gradient to right side | |
CGContextDrawLinearGradient | |
( | |
context, // A Quartz graphics context. | |
gradDark, // A CGGradient object. | |
CGPointMake(w, 0), // Starting point of the gradient. | |
CGPointMake(w-(m*2), 0), // Ending point of the gradient. | |
kCGGradientDrawsBeforeStartLocation | |
); | |
// applies gradient to left side | |
CGContextDrawLinearGradient | |
( | |
context, // A Quartz graphics context. | |
gradLight, // A CGGradient object. | |
CGPointMake(0, 0), // Starting point of the gradient. | |
CGPointMake((m*2), 0), // Ending point of the gradient. | |
kCGGradientDrawsBeforeStartLocation | |
); | |
// applies gradient to top side | |
CGContextDrawLinearGradient | |
( | |
context, // A Quartz graphics context. | |
gradLight, // A CGGradient object. | |
CGPointMake(0, h), // Starting point of the gradient. | |
CGPointMake(0, h-(m*2)), // Ending point of the gradient. | |
kCGGradientDrawsBeforeStartLocation | |
); | |
// convert context back to UIImage | |
imageRef = CGBitmapContextCreateImage(context); | |
image = [UIImage imageWithCGImage:imageRef]; | |
CGImageRelease(imageRef); | |
// frees resources | |
CGGradientRelease(gradDark); | |
CGGradientRelease(gradLight); | |
CGContextRelease(context); | |
CGColorSpaceRelease(color); | |
return(image); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment