views:

225

answers:

1

I have saved a bunch of CGContextRefs and I want to draw all of these out in the drawRect portion of my UIView. How can I do this?

A: 

Here is one way:

- (void)drawRect:(CGRect)rect {
    CGImageRef newImg = CGBitmapContextCreateImage(ctx1);
    [[UIImage imageWithCGImage:newImg] drawInRect:rect];
    CGImageRelease(newImg);
    CGImageRef newImg = CGBitmapContextCreateImage(ctx2);
    [[UIImage imageWithCGImage:newImg] drawInRect:rect];
    CGImageRelease(newImg);
}
mahboudz
Thanks. I have been reading about Graphics States. I'm just trying to save individual brush strokes so would saving and restoring graphics states be a faster way to do this?
RexOnRoids
And how do I draw the resulting image in -drawRect?
RexOnRoids
That's it. The resulting images are being drawn on top of each other into the rect provided to drawRect.
mahboudz
You probably want to add blendmodes, or have the images draw next to each other instead of on top, but that's the gist of it. I'm not sure if this is faster than drawing individual brush strokes, but it does allow you to do some of the work at other times and then dump it all to the screen at drawRect time. There are also other paths to take from CGImageRef to drawing that won't have you turning the context back to an UIImage, which may speed things up.
mahboudz