Undo Redo Issues with Cglayer

Undo/Redo for drawing in iOS

I have found a solution for this, we need to create a array of array of DrawingPaths:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Do the above code, then
[m_undoArray addObject:self.currentPath];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[m_parentUndoArray addObject:[NSArray arrayWithArray:m_undoArray]];
}

and then stroke the path in DrawRect.

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

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

Memory issues with CGlayer

Your permanentDrawingLayer is a CGLayerRef so setting it to NULL, don't release it.
You need to call CGLayerRelease(permanentDrawingLayer) before setting it to NULL.

- (void)IncreaseCanavasSize
{
CGContextRef layerContext1 = CGLayerGetContext(permanentDrawingLayer );
CGContextDrawLayerInRect(layerContext1, rectSize, newDrawingLayer);

rectSize = self.bounds;

CGFloat scale = self.contentScaleFactor;
CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
CGLayerRef layer = CGLayerCreateWithContext(layerContext1, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextScaleCTM(layerContext, scale, scale);
[self setNewDrawingLayer:layer];
CGLayerRelease(layer);

CGContextDrawLayerInRect(layerContext, self.bounds, permanentDrawingLayer);

CGLayerRelease(permanentDrawingLayer);
permanentDrawingLayer = NULL;
}

Also if your method -setPermanentDrawingLayer: looks like the method -setCurrentDrawingLayer: in your previous question.
In -IncreaseCanavasSize You can simply replace the line permanentDrawingLayer = nil; with [self setPermanentDrawingLayer:NULL];, then it will release the CGLayerRef and set it to NULL.

How to Undo & Redo a smooth line in iPhone?

Use the NSUndoManager. However, if you are painting lines on the canvas, you will also need to keep their representation around as well (so you can pop them off).

So, whether you collect them as a UIBezierPath, or use shape layers, or your own "array of points" you undo in the same manner.

So, while drawing the line, keep a record of the points you used in your drawing. When the drawing is done (e.g., touchesEnded), you want to "push" your drawing, and tell the undo manager how to undo it. Simply, it would be something like this almost-code...

- (void)pushDrawing:(Drawing*)drawing
{
[self.stack push:drawing];
[self.undoManager registerUndoWithTarget: self
selector: @selector(popDrawing)
object: nil];
}

- (void)popDrawing:(Drawing*)drawing
{
Drawing *drawing = [self.stack pop];
[self.undoManager registerUndoWithTarget: self
selector: @selector(pushDrawing:)
object: drawing];
}

If you are using one canvas, you may have to redraw the entire thing, especially when popping a drawing off. If you are using views or layers, you may not...

Look at the docs for NSUndoManager... it's available on iOS, and has good examples. It "remembers" if you you undo-ing or redo-ing, and will do the right thing, so the above could be implemented as one function (but it's easier to understand at first with one function going each direction).

CGLayerRef in NSValue - when to call retain() or release()?

NSValue doesn't know about Core Foundation types, so it will not retain or release the CGLayer. (The @encoded type string pretty much just tells it how big the value is; it does not tell it anything about memory management.)

You must not release the layer until you are fully done with both the layer and the NSValue.

Or, better yet, just put the CGLayer into the array. All Core Foundation objects are compatible with NSObjects for purposes of memory management (discussed previously), which has the practical effect that you can put CF objects into NSArrays and vice versa. Since CGLayers are CF objects, this means that you can put the CGLayer into the array without boxing it in another object.

Getting blur stroked while drawing in Cglayer

Substitute this in your code where you create your CGLayer:

if(self.currentDrawingLayer == nil)
{
CGFloat scale = self.contentScaleFactor;
CGRect bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextScaleCTM(layerContext, scale, scale);
self.currentDrawingLayer = layer;
}


Related Topics



Leave a reply



Submit