iphonequartz-graphicscglayer

iPhone - pushing and pulling a value from a dictionary to a CGlayer


I have a finger painting view that I am implementing undo/redo. All the drawing occurs on a CGLayer called lineLayer.

As soon as the user touches the screen but before any modification is done to the screen, I need to quickly grab the views content and create an undo level.

Obviously saving the view to disk is not an option, because it takes too long. The solution I imagined is to copy the CGLayer to a dictionary, using something like this:

// I copy the layer as a NSValue
NSValue *layerCopy = [NSValue valueWithBytes:&myLayer objCType:@encode(CGLayerRef)];

I store it on a dictionary

NSString *aKey = [NSString stringWithFormat:@"%d", [number intValue]];
[self.dictUNDO setObject:layerCopy forKey:aKey];

later I retrieve it using

NSString *myKey = [NSString stringWithFormat:@"%d", [number intValue]];
NSValue *myCopy = [self.dictUNDO objectForKey:myKey]; 
[myCopy getValue:&lineLayer];
//lineLayer is the CGLayer where the drawing happens

the problem is that lineLayer is not changed at all by this reading operation. It continues to have the same contents as before.

Am I missing something?

thanks.


Solution

  • Have you considered Core data for managing your objects? I'm asking because Undo/redo comes for free with it?