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

用CGlayer绘图的慢性能
EN

Stack Overflow用户
提问于 2014-03-04 15:43:47
回答 1查看 833关注 0票数 3

我正在使用CGlayers进行绘图,正如文档所述,这是在画布上绘制绘图的更有效的方法。

基本上,我使用CGContextDrawLayerInRect绘制到CGlayers,并将图层绘制到图形上下文中。

这是我的drawRect方法

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect
{      
   switch (m_drawStep)
   {
       case DRAW:
       {               
           CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw)

           if(self.currentDrawingLayer == nil)//Potential leak of memory- static analyzer
           {
               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;
           }           

           CGContextRef layerContext = CGLayerGetContext(self.currentDrawingLayer);//Potential leak of memory- static analyzer
           CGContextBeginPath(layerContext);
           CGContextAddPath(layerContext, mutablePath);
           CGContextSetLineWidth(layerContext, self.lineWidth);
           CGContextSetStrokeColorWithColor(layerContext, self.lineColor.CGColor);
           CGContextSetFillColorWithColor(layerContext, self.lineColor.CGColor);
           CGContextSetBlendMode(layerContext,kCGBlendModeNormal);
           CGContextStrokePath(layerContext);
           CGPathRelease(mutablePath);

           CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer );//Potential leak of memory- static analyzer
       }
           break;

       case UNDO:
       {                          
           CGContextRef context = UIGraphicsGetCurrentContext();           

           if(self.currentDrawingLayer == nil)//Potential leak of memory- static analyzer
           {
               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;
           }                                

           CGContextRef layerContext1 = CGLayerGetContext(self.currentDrawingLayer );//Potential leak of memory- static analyzer
           CGContextClearRect(layerContext1, self.bounds);

          for(NSArray *undoArray in m_parentUndoArray)
          {
               for(int i =0; i<[undoArray count];i++)
              {
                   DrawingPath *drawPath = [undoArray objectAtIndex:i];
                   CGPathRef path = drawPath.path.CGPath;
                   mutablePath = CGPathCreateMutableCopy(path);


                   CGContextBeginPath(layerContext1);
                   CGContextAddPath(layerContext1, mutablePath);
                   CGContextSetLineWidth(layerContext1, drawPath.pathWidth.floatValue);
                   CGContextSetStrokeColorWithColor(layerContext1, drawPath.pathColor.CGColor);

                   if([drawPath.pathColor isEqual:[UIColor clearColor]])
                   {
                        CGContextSetBlendMode(layerContext1,kCGBlendModeClear);
                   }
                   else
                   {
                        CGContextSetBlendMode(layerContext1,kCGBlendModeNormal);
                   }

                       CGContextStrokePath(layerContext1);
                       CGPathRelease(mutablePath);
                   }
               }

                 CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer);//Potential leak of memory- static analyzer
           }
        }
           break;



    [super drawRect:rect];
}

在我的移动函数函数中,我创建了UIBezeirPath并将其转换为CGPath。

代码语言:javascript
复制
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{          
   self.currentPath = [[DrawingPath alloc] init];

   if(m_eraseButtonClicked)
   {
      [self.currentPath setPathColor:[UIColor clearColor]];         
   }
   else
   {
        [self.currentPath setPathColor:self.lineColor];            
   }

   CGPathRef cgPath = self.currentPath.path.CGPath;
    mutablePath = CGPathCreateMutableCopy(cgPath);

        [m_undoArray addObject:self.currentPath];
        [self setNeedsDisplay];

  }

触碰结束

代码语言:javascript
复制
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   [m_parentUndoArray addObject:[NSArray arrayWithArray:m_undoArray]];

}

我所面临的问题如下

1)使用时间分析器进行了测试,CGContextDrawLayerInRect,在绘制过程中占用了90%的时间,因此我想知道如何减少这种方法所花费的时间,并优化drawRect方法。

2)如果我画了几条很长的线,并开始连续地撤消/重做,那么您也可以看到CGContextDrawLayerInRect,它需要很多时间。

3)如果我删除了一些画好的线,并开始重复进行撤消/重做,更糟糕的是,我的应用程序会崩溃,并发出内存警告,我不知道擦除程序出了什么问题,它占用了那么多内存。

编辑:代码,更新后显示静态分析器在哪里显示它们是内存问题。

EN

回答 1

Stack Overflow用户

发布于 2017-08-10 20:09:01

从我自己的实验来看,iOS有时会使用GPU,有时使用CGContextDrawLayerInRect的CPU,这取决于被复制的层的大小(以及其他可能的因素)。当它使用CPU时,它会慢20倍。

唯一的解决方案是减少更新区域的大小,方法是使用setNeedsDisplayInRect而不是setNeedsDisplay,并在drawRect代码中添加CGContextClipToRect(context,rect)。

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

https://stackoverflow.com/questions/22176467

复制
相关文章

相似问题

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