CGContextDrawLayerInRect

CGContextDrawLayerInRect

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

我基本上是绘制到CGlayers中,然后使用将层绘制到图形上下文中CGContextDrawLayerInRect

这是我的drawRect方法

- (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];
}

在我的Moved函数功能中,我创建了UIBezeirPath并将其转换为CGPath。
-(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];

  }

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

}

我面临的问题如下

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

2)如果我画了一些长的线并开始连续进行撤消/重做,那么你也可以看到 CGContextDrawLayerInRect ,这会花费很多时间。

3)如果我擦除了一些绘制的线条并开始重复执行撤消/重做操作,更糟糕的是,我的应用程序崩溃并发出内存警告,我不知道擦除出了什么问题,它占用了太多内存。

编辑:代码,更新以显示静态分析器在哪里说这是内存问题

最佳答案

根据我自己的实验,iOS有时会针对CGContextDrawLayerInRect使用GPU,有时会使用CPU,具体取决于要复制的图层的大小(以及其他可能的因素)。使用CPU时,速度要慢20倍。

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

关于ios - CGlayer Drawing性能降低,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22176467/

10-13 03:51