Created
January 31, 2013 12:18
-
-
Save swissmanu/4682477 to your computer and use it in GitHub Desktop.
Following functions make the the creation and drawing of programmatical generated UIImages a little bit easier. Call `CreateBitmapContext(CGSize)` to get a CGContextRef you can draw into. Afterwards, just call `CreateUIImageFromBitmap(CGContextRef)` to return a common usable UIImage instance and free up any used resources. This Gist is based on h…
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
CGContextRef CreateBitmapContext(CGSize size) { | |
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef ctx = CGBitmapContextCreate(nil, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast); | |
CGColorSpaceRelease(space); | |
return ctx; | |
} | |
UIImage* CreateUIImageFromBitmapContext(CGContextRef ctx) { | |
CGImageRef cgImage = CGBitmapContextCreateImage(ctx); | |
UIImage *image = [UIImage imageWithCGImage:cgImage]; | |
CGImageRelease(cgImage); | |
CGContextRelease(ctx); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment