Created
September 10, 2013 15:02
-
-
Save liamnichols/6510720 to your computer and use it in GitHub Desktop.
UIColor to UIImage
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
#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