Create new UIImage by adding shadow to existing UIImage

Posted by Tom Irving on Stack Overflow See other posts from Stack Overflow or by Tom Irving
Published on 2010-05-29T20:10:54Z Indexed on 2010/05/29 20:12 UTC
Read the original article Hit count: 557

Filed under:
|
|
|
|

I've taken a look at this question: http://stackoverflow.com/questions/962827/uiimage-shadow

But the accepted answer didn't work for me.

What I'm trying to do is take a UIImage and add a shadow to it, then return a whole new UIImage, shadow and all.

This is what I'm trying:

- (UIImage*)imageWithShadow {

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width, self.size.height + 1, CGImageGetBitsPerComponent(self.CGImage), 0, 
                                                 colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadow(shadowContext, CGSizeMake(0, -1), 1);
    CGContextDrawImage(shadowContext, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);

    CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
    CGContextRelease(shadowContext);

    UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
    CGImageRelease(shadowedCGImage);

    return shadowedImage;
}

The result is that I get exactly the same image as before I put it through this method.

I am doing this the correct way, or is there something obvious I'm missing?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c