Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Created January 31, 2013 12:18
Show Gist options
  • Save swissmanu/4682477 to your computer and use it in GitHub Desktop.
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…
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