make round corners for UIImage

November 14, 2009

- (UIImage *) roundCorners: (UIImage*) img
{
int w = img.size.width;
int h = img.size.height;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
addRoundedRectToPath(context, rect, 5, 5);
CGContextClosePath(context);
CGContextClip(context);

CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
[img release];

return [UIImage imageWithCGImage:imageMasked];
}

Categories: Uncategorized.

Comment Feed

No Responses (yet)



Some HTML is OK

or, reply to this post via trackback.