Skip to content

Instantly share code, notes, and snippets.

@liamnichols
Created September 10, 2013 15:02
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 liamnichols/6510720 to your computer and use it in GitHub Desktop.
Save liamnichols/6510720 to your computer and use it in GitHub Desktop.
UIColor to UIImage
#pragma mark - UIImage Representations
- (UIImage *)imageRepresentation
{
CGFloat scale = [[UIScreen mainScreen] scale];
CGRect rect = CGRectMake(0, 0, scale, scale);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, CGRectGetWidth(rect), CGRectGetHeight(rect), 8, 0, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(rgbColorSpace);
CGContextSetFillColorWithColor(context, [self CGColor]);
CGContextFillRect(context, rect);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *image = [UIImage imageWithCGImage:cgImage scale:scale orientation:UIImageOrientationUp];
CGImageRelease(cgImage);
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment