Skip to content

Instantly share code, notes, and snippets.

@jamztang
Created January 6, 2012 18:48
Show Gist options
  • Select an option

  • Save jamztang/1571875 to your computer and use it in GitHub Desktop.

Select an option

Save jamztang/1571875 to your computer and use it in GitHub Desktop.
Creating a placeholder UIImage dynamically with color
//
// UIImage+JTColor.h
//
// Created by james on 8/22/11.
//
#import <UIKit/UIKit.h>
@interface UIImage (JTColor)
+ (UIImage *)imageWithColor:(UIColor *)color;
@end
//
// UIImage+JTColor.m
//
// Created by james on 8/22/11.
//
#import "UIImage+JTColor.h"
@implementation UIImage (JTColor)
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
// Create a 1 by 1 pixel context
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect); // Fill it with your color
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@klauslanza
Copy link

Loving it, hope it stays here so I can use it in my podfiles!

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