Skip to content

Instantly share code, notes, and snippets.

@sdpjswl
Created June 17, 2015 06:22
Show Gist options
  • Save sdpjswl/0e268f72de24341c579d to your computer and use it in GitHub Desktop.
Save sdpjswl/0e268f72de24341c579d to your computer and use it in GitHub Desktop.
Add a shadow to a UIImage
- (UIImage *)imageWithShadow {
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGFloat imageWidth = self.size.width;
CGFloat imageHeight = self.size.height;
CGContextRef shadowContext = CGBitmapContextCreate(NULL, imageWidth, imageHeight, CGImageGetBitsPerComponent(self.CGImage), 0, colourSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
CGContextSetShadowWithColor(shadowContext, CGSizeMake(1, -1), 3, [UIColor greenColor].CGColor);
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment