views:

589

answers:

1

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?

+3  A: 

There are several problems with your code:

  • The target image is too small. Be sure there enough place to draw the shadow.
  • Consider using CGContextSetShadowWithColor to define both the shadow and its color.
  • Don't forget that coordinate system is flipped, so the origin is bottom-left, not top-left.

By fixing these issues, the shadow should be drawn.

- (UIImage*)imageWithShadow {
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef shadowContext = CGBitmapContextCreate(NULL, self.size.width + 10, self.size.height + 10, CGImageGetBitsPerComponent(self.CGImage), 0, 
                                             colourSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    CGContextSetShadowWithColor(shadowContext, CGSizeMake(5, -5), 5, [UIColor blackColor].CGColor);
    CGContextDrawImage(shadowContext, CGRectMake(0, 10, self.size.width, self.size.height), self.CGImage);

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

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

    return shadowedImage;
}
Laurent Etiemble
Perfect! I was actually adding 1 to the height to draw the shadow, but the other stuff (setting the shadow, flipped coordinate system) was spot on.
Tom Irving