Created
October 6, 2009 17:02
-
-
Save coneybeare/203202 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 *)addBorderToImage:(UIImage *)image { | |
CGImageRef bgimage = [image CGImage]; | |
float width = CGImageGetWidth(bgimage); | |
float height = CGImageGetHeight(bgimage); | |
// Create a temporary texture data buffer | |
void *data = malloc(width * height * 4); | |
// Draw image to buffer | |
CGContextRef ctx = CGBitmapContextCreate(data, | |
width, | |
height, | |
8, | |
width * 4, | |
CGImageGetColorSpace(image.CGImage), | |
kCGImageAlphaPremultipliedLast); | |
CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage); | |
//Set the stroke (pen) color | |
CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor); | |
//Set the width of the pen mark | |
CGFloat borderWidth = (float)width*0.05; | |
CGContextSetLineWidth(ctx, borderWidth); | |
//Start at 0,0 and draw a square | |
CGContextMoveToPoint(ctx, 0.0, 0.0); | |
CGContextAddLineToPoint(ctx, 0.0, height); | |
CGContextAddLineToPoint(ctx, width, height); | |
CGContextAddLineToPoint(ctx, width, 0.0); | |
CGContextAddLineToPoint(ctx, 0.0, 0.0); | |
//Draw it | |
CGContextStrokePath(ctx); | |
// write it to a new image | |
CGImageRef cgimage = CGBitmapContextCreateImage(ctx); | |
UIImage *newImage = [UIImage imageWithCGImage:cgimage]; | |
CFRelease(cgimage); | |
CGContextRelease(ctx); | |
// auto-released | |
return newImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great, but dont forget to add this: