Skip to content

Instantly share code, notes, and snippets.

@kompozer
Forked from ktakayama/UIImage+shadow.m
Created May 2, 2010 15:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kompozer/387210 to your computer and use it in GitHub Desktop.
Save kompozer/387210 to your computer and use it in GitHub Desktop.
@interface UIImage (Shadow)
+ (UIImage *)imageWithContentsOfFile:(NSString *)path withShadow:(BOOL)applyShadow;
+ (UIImage *)imageNamed:(NSString *)name withShadow:(BOOL)applyShadow;
+ (UIImage *)applyShadow:(UIImage *)theImage;
@end
@implementation UIImage (Shadow)
+ (UIImage *)imageWithContentsOfFile:(NSString *)path withShadow:(BOOL)applyShadow {
UIImage *image = [UIImage imageWithContentsOfFile:path];
if (!applyShadow) {
return image;
}
return [UIImage applyShadow:image];
}
+ (UIImage *)imageNamed:(NSString *)name withShadow:(BOOL)applyShadow
{
UIImage *image = [UIImage imageNamed:name];
if (!image) {
return image;
}
if (!applyShadow) {
return image;
}
return [UIImage applyShadow:image];
}
+ (UIImage *)applyShadow:(UIImage *)theImage
{
UIGraphicsBeginImageContext(CGSizeMake(theImage.size.width + 12, theImage.size.height + 12));
CGContextSetShadow(UIGraphicsGetCurrentContext(), CGSizeMake(6.0f, -6.0f), 6.0f);
[theImage drawAtPoint:CGPointZero];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
@end
@kompozer
Copy link
Author

kompozer commented May 2, 2010

Adds a shadow to an UIImage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment