Skip to content

Instantly share code, notes, and snippets.

@burczyk
Created August 2, 2013 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burczyk/6138360 to your computer and use it in GitHub Desktop.
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.
#import <UIKit/UIKit.h>
@interface UIImage (Color)
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;
@end
#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