Skip to content

Instantly share code, notes, and snippets.

@rowanj
Forked from troystribling/UIImage+Extensions.h
Last active December 16, 2015 18:39
Show Gist options
  • Save rowanj/5479492 to your computer and use it in GitHub Desktop.
Save rowanj/5479492 to your computer and use it in GitHub Desktop.
Make a blank UI image with a given size, and optional fill color
#import <Foundation/Foundation.h>
@interface UIImage (Extensions)
+ (UIImage*)blankImage:(CGSize)_size;
+ (UIImage*)blankImage:(CGSize)_size withColor:(UIColor*)_color;
@end
#import "UIImage+Extensions.h"
@implementation UIImage (Extensions)
+ (UIImage*)blankImage:(CGSize)_size {
return [self blankImage:_size withColor:nil];
}
+ (UIImage*)blankImage:(CGSize)_size withColor:(UIColor*)_color {
if (!_color) {
_color = [UIColor clearColor];
}
UIGraphicsBeginImageContext(_size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, _color.CGColor);
CGContextFillRect(context, CGRectMake(0.0, 0.0, _size.width, _size.height));
UIImage* outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment