Graphics Programs Reference
In-Depth Information
Touch (as you did when you created the thumbnails for the possessions), you typically do
something like this:
// Create context
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
... Do drawing here ...
// Get image result
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
// Clean up image context
UIGraphicsEndImageContext();
A bitmap context is created and drawn to, and the resulting pixels are stored in a
UIImage instance.
The UIGraphics suite of functions provides a convenient way of creating a bitmap
CGContextRef and writing that data to a UIImage object:
// Create a color space to use for the context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a context of appropriate width and height
// with 4 bytes per pixel - RGBA
CGContextRef ctx =
CGBitmapContextCreate(NULL, width, height, 8, width * 4,
colorSpace, kCGImageAlphaPremultipliedLast);
// Make this context the current one
UIGraphicsPushContext(ctx);
... Do drawing here ...
// Get image result
CGImageRef image = CGBitmapContextCreateImage(ctx);
UIImage *result = [[[UIImage alloc] initWithCGImage:image] autorelease];
// Clean up image context - make previous context current if one exists
UIGraphicsPopContext();
CGImageRelease(image);
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
A layer creates the same kind of context when it needs to redraw its contents. However, a
layer does it a little differently. See the NULL as the first parameter to CGBitmapCon-
textCreate ? That is where you pass a data buffer to hold the pixels generated by
drawing routines in this context. By passing NULL , we say, “Core Graphics, figure out
how much memory is needed for this buffer, create it, and then dispose of it when the con-
text is destroyed.” A CALayer already has a buffer (its contents), so it would call the
function as follows:
CGContextRef ctx =
CGBitmapContextCreate(myBitmapPixels, width, height, 8, width * 4,
colorSpace, kCGImageAlphaPremultipliedLast);
Search WWH ::




Custom Search