Created
August 2, 2013 08:34
-
-
Save burczyk/6138360 to your computer and use it in GitHub Desktop.
UIImage shorthand to create UIImage with specified color and size. It can be used e.g. to stylize UITabBar or UINavigationBar with single-color image in place.
This file contains 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
#import <UIKit/UIKit.h> | |
@interface UIImage (Color) | |
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size; | |
@end |
This file contains 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
#import "UIImage+Color.h" | |
@implementation UIImage (Color) | |
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size { | |
UIImage *img = nil; | |
@autoreleasepool { | |
CGRect rect = CGRectMake(0, 0, size.width, size.height); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetFillColorWithColor(context, color.CGColor); | |
CGContextFillRect(context, rect); | |
img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
} | |
return img; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment