I am working with CGlayer drawing, I am using 3 CGlayers for drawing, named as temporary, permanent and newLayer.
1) While drawing, I draw first into temporary Layer and on touchesEnded, I transfer data from temporary to permanent Layer and clear out temporay Layer
Here is the code
CGContextRef layerContext = CGLayerGetContext(self.permanentDrawingLayer );
CGContextDrawLayerInRect(layerContext, self.bounds, self.temporayDrawingLayer);
CGContextRef ctx = CGLayerGetContext(self.temporayDrawingLayer);
CGContextClearRect(ctx, self.bounds);
Now my canvas size is dynamic, user can increase/decrease it, so whenever, user increases the canvas size, I first create a new Layer called newDrawingLayer draw the drawing from pemanentLayer into this NewDrawingLayer and destroy permanent Layer
Now when user tries to erase, when he clicks on erase button, I transfer data from permanentLayer to CurrentLayer and then do erase
Here is my drawRect method
- (void)drawRect:(CGRect)rect
{
switch (m_drawStep)
{
case DRAW:
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(self.temporayDrawingLayer == nil)
{
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
self.temporayDrawingLayer = layer;
}
if(self.permanentDrawingLayer == nil)
{
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
self.permanentDrawingLayer = layer;
}
CGContextDrawLayerInRect(context,rectSize, self.newDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.permanentDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.temporayDrawingLayer);
}
break;
case ERASE:
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(self.temporayDrawingLayer == nil)
{
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
self.currentDrawingLayer = layer;
}
if(self.permanentDrawingLayer == nil)
{
CGLayerRef layer = CGLayerCreateWithContext(context, bounds.size, NULL);
self.permanentDrawingLayer = layer;
}
if(self.newDrawingLayer == nil)
{
self.newDrawingLayer = CGLayerCreateWithContext(context, self.bounds.size, NULL);
}
CGContextDrawLayerInRect(context, rectSize, self.newDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.permanentDrawingLayer);
CGContextDrawLayerInRect(context, self.bounds, self.temporayDrawingLayer);
}
break;
}
The problems I am facing are
1) Whenever I erase the drawing by increasing the canvas size and then try to draw again, my drawing is extremely slow, which is not at all acceptable.
2) If I dont increase the canvas size and then perform drawing and erasing continously, I dont have any issues.
What can be the reason that the drawing gets so slow, I think, because, system is trying to draw three Layers, or is it something else, that I am not aware of, please help me out friends. If any doubts, please ask, I will try to explain it better.