Search code examples
iphoneioscore-graphicsquartz-2dcgimage

On iOS, is there a way to directly alter or draw to CGImage instead of drawing to Graphics Context and then convert the whole thing?


If we already have a Bitmap Graphics Context, and we converted this context to a CGImage. Now we want to add a single dot to the CGImage. Can we alter the CGImage directly, instead draw a single dot to the graphics context and covert the whole context once again to a CGImage?

The idea is that CGImage is also a structure, so if we can alter some data in the structure, that should somehow be possible?


Solution

  • CGImages are immutable. They cannot be changed after they are created.

    If we already have a Bitmap Graphics Context, and we converted this context to a CGImage

    CGBitmapContextCreateImage doesn't "convert" a context to an image -- it effectively takes a snapshot of the current state of the context.

    You can draw more things in the original context. (The first CGImage will not be affected.) Then call CGBitmapContextCreateImage again, to get a new image with the new drawing in it.