Created
June 17, 2015 06:22
-
-
Save sdpjswl/0e268f72de24341c579d to your computer and use it in GitHub Desktop.
Add a shadow to a UIImage
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 *)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