1

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.

4

1 回答 1

0

听起来您有一个非常复杂的系统,您需要在其中将图层内容从一层复制到另一层。那会很贵。

当用户更改画布大小时,您是说绘图很慢,然后恢复,还是说绘图永远很慢?

无论如何,我建议使用工具应用程序来分析您的代码并找出哪些部分是瓶颈。然后,您可以使用该信息来确定在哪里花费时间进行优化。

于 2014-03-06T14:59:11.913 回答