drawing into an existing CGImage

Ok, maybe I'm hung up on Mac OS X design patterns, just tell me if that's the case.


the problem:

I need to draw a line on an existing CGImage.

There's no LockFocus methods for CGImage, There's no obvious documented way to get the already created Context of a Bitmap in the CGImage. There's no obvious or explicit code explaining the procedure.


I have the reference to the image, I have the two points that make up the line, I know all of the code to draw that line... I just cannot make the context associated to that image, the current context. What gives? what is the design pattern? Please tell me where I can read about this.

Replies

I refuse to believe that while I am drawing (following the Touch) I need to copy the ENTIRE image into a new image.

That is rediculous, yet, that's what all of the examplers show: copy the image into a new context, and then do your drawing before you end the context, and capture it in the image.

Refuse to believe it if you like, but it's the truth. CGImages are explicitly immutable.


You could create a bitmap context, draw the image into it, then draw the line, and create a new image from that.


Alternatively, it sounds like your use case is better suited to just using the bitmap context as your main object. Keep it around and draw into it whenever you want to "change the image". Just at the point where you want a CGImage to store to file, transmit over a network, or draw elsewhere, create a CGImage from the bitmap context, use it, and release it.

  • Hi Ken, is it also possible to use a CALayer instead of a bitmap context to repeatedly draw over that same original CGImage? will it be faster? how does one that?

Add a Comment

Hi Ken.

I was able to implement a draw loop that creates a new CGImage for each call to touchesMoved(), that largely does what i would like.


There are some problems, which are frankly: mistifying. the worst one being:

my touches are mirrored across the Y axis.

I take the raw touches set and stamp each one of them. This results in vertical mirroring of the line. I don't even know where to start looking for help fixing that. I feel stupid asking if it's expected behavior, there's no way this should be expected behavior. And like I said: I'm not changing the list of touches in any way.


but back to use case, because I think this is causing a rats nest of other (I won't call them minor, but I suspect that they are related) issues. There's another consideration for the use case: I'm feeding the image into the content property of a CALayer. It makes for very good zoom/pan/rotate manipulation.

it is my understanding that CALayer requires a CGImage (in the boundaries of our discussion) So while I'd love to keep the context around, in order to display it, I still need to make the CGimage, and apply it... during each call to the touchesMoved function. Currently I am copying the current image into a new Context, but I did try to maintain a Context, and CALayer did not like that. I'm in bootstrap mode, as I try to make sense out of IOS' design patterns. So I'm making prototypes as quickly as I can.

  • Did you finally find a decent way to draw a line over your CGImage (in your scenario and context) ? I'd LOVE to see how. I have similar task, but I'm on the Mac, and my "line"(s) aren't coming from user touches, but rather from some image analysis that finds shapes in the image, and I don't need to ever draw the thing onscreen - just process the image and produce another image. Still - I'd like to be efficient and not copy the whole (huge! in my case) image every time I need to draw a line!!!.

    Have you found the optimized way to work with this? BTW -- I think if you have a CALayer with a CGImage, you should be able to continuously draw over the layer itself, and the final "merging" of the layer with its image will only be done when you "draw" (or otherwise "output") an image from that. I don't know how to do any of this (or I wouldn't be writing this) but I think that might work?

    In anyway, be kind to add your own answer hopefully with little helpful code sample. Thanks

Add a Comment