3

We're repetitively making a CGLayer, doing processing, and then releasing it. This happens a lot in real time. Surely there is a lot of overhead in making a whole new CGLayer each time. So...

Surely it would be better to just keep the layer around, and erase all the data from it each time -- rather than creating a new one from scratch.

Note: if you paint in a blank or clear rectangle covering everything, that just adds even more data on top of your extant paths.

So, how to actually "erase" or "start again" a CGLayer?

There is a function CGContextBeginPath(cc) but it's confusing: it seems to only clear out "that" path, it does not appear to erase all of the CGLayer back to no-data state.

How to return a CGLayer to a state of no-data? Does anyone know?

Update...

It turns out there is actually NO WAY TO DO THIS.

After considerable experimentation, we have determined that there appears to be no way to clear out all the data from a CGLayer (which is disappointing really).

Note that adding a new white or clear rectangle, only does that - it actually adds more data.

So unfortunately no known way to do this. If you are building these at high hz (perhaps for a calculation), you just have to start with a fresh one each time. Or, you can apparently delete (actually delete, not just cover) just the one path using CGContextBeginPath().

Hopefully this will help someone in the future.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Hello @Joe Blow, I am also stuck with this issue of clearing data from CGlayer, my problem is that, I erase in one Layer and write in another layer, So whenever user switches between pen and erase, I draw one layer into another and clear the previous Layer using method,CGContextClearRect(), but what happens is that, if I dont erase the writing, and then again click on pen, but dont write and then click on erase again, and try to erase, then all writing gets cleared. How can we solve this, My SO question is here http://stackoverflow.com/questions/22201208/draw-one-cglayer-into-another-cglayer – Ranjit Mar 06 '14 at 07:24

2 Answers2

2

Once you have the context call CGContextClearRect( cc, someRect ) to clear the contents.

Jon Steinmetz
  • 4,104
  • 1
  • 23
  • 21
  • To be clear Jon this **does not** actually **remove the data**. It just adds (another) clear slice of data on top. – Fattie Jan 13 '11 at 22:13
  • 1
    I don't understand why this is the right answer if the question is how do you erase the data. – Ant Jul 11 '13 at 11:48
1

Why don't you just fill it with a rectangle of (clear/white) color? Make sure the layer is not opaque if you wanna clear it with clearColor (transparent).

iwat
  • 3,591
  • 2
  • 20
  • 24
  • How about backing up initial GState and restore it once you are done? So the process will be 0) create `CGLayer` 1) saveGState 2) do what you want 3) restoreGState 4) clear screen 5) restart from (1) – iwat Jan 06 '11 at 03:34
  • Hi iWat! don't forget the graphics state only involves things like **pen colour, thickness of the line, background colour** and so on --- unfortunately *NOT* the actual paths and data! – Fattie Jan 06 '11 at 08:32