Search code examples
iphonequartz-2d

Quartz2D how to save the current context and then draw over it?


I'm trying to draw using Quartz2D and but i'm finding the drawing to lag a lot right after a few dots. Therefore I am wondering how can I cache the current context into a bmp or a jpg or a layer and then draw onto the layer again to make the drawing smoother. Which way is the best way to do it? Thanks!


Solution

  • IIRC, Quartz2D uses a painting model to render content, and once you draw something you can't interact with it at all, so I don't see why caching the current context would do anything to help.

    However, if you want to cache your current context, the code looks something like this:

    // Begin image context
    UIGraphicsBeginImageContext(view.bounds.size);
    
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGImageRef screenshotRef = CGBitmapContextCreateImage(UIGraphicsGetCurrentContext());
    UIImage *screenshot = [[UIImage alloc] initWithCGImage:screenshotRef];
    
    // End image context
    UIGraphicsEndImageContext();
    // Release CGImage
    CGImageRelease(screenshotRef);
    
    return screenshot;