首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGlayer绘图变慢

CGlayer绘图变慢
EN

Stack Overflow用户
提问于 2014-03-06 14:40:01
回答 1查看 352关注 0票数 1

我正在使用CGlayer绘图,我使用3 CGlayers进行绘图,命名为临时的、永久的和newLayer.

1)在绘制过程中,先绘制到临时层,然后在touchesEnded上将数据从临时层传输到永久层,并清除临时层。

这是代码

代码语言:javascript
复制
CGContextRef layerContext = CGLayerGetContext(self.permanentDrawingLayer );
CGContextDrawLayerInRect(layerContext, self.bounds, self.temporayDrawingLayer);
CGContextRef ctx = CGLayerGetContext(self.temporayDrawingLayer);
            CGContextClearRect(ctx, self.bounds);

现在我的画布大小是动态的,用户可以增加/减少它,所以每当用户增加画布大小时,我首先创建一个名为的新层,名为newDrawingLayer,将从pemanentLayer中提取的图绘制到这个NewDrawingLayer中,并破坏永久层。

现在,当用户尝试擦除时,当他单击擦除按钮时,我将数据从permanentLayer传输到CurrentLayer,然后执行擦除操作。

这是我的drawRect方法

代码语言:javascript
复制
- (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;
}

我面临的问题是

1)每当我增加画布的尺寸,然后再画一次,我的画就会非常慢,这是完全不能接受的。

2)如果我不增加画布的尺寸,然后继续画画和擦除,我就没有任何问题了。

为什么画得这么慢,我想,因为,系统正在试图绘制三层,或者是其他的东西,我不知道,请帮助我的朋友。如果有任何疑问,请问一问,我会尽量解释得更好。

EN

回答 1

Stack Overflow用户

发布于 2014-03-06 14:59:11

听起来好像你有一个非常复杂的系统,在那里你要从一个层复制到另一个层的内容。那会很贵的。

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

无论如何,我建议使用仪器应用程序来分析您的代码,并找出瓶颈是哪些部分。然后,您可以使用这些信息来确定将您的时间花在优化上的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22227647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档