【问题标题】:Slow performance with CGlayer DrawingCGlayer 绘图性能缓慢
【发布时间】:2014-03-04 15:43:47
【问题描述】:

我正在使用 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];
}

在我的 touches 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) 如果我擦除一些绘制的线条并开始重复撤消/重做,更糟糕的是,我的应用程序崩溃并出现内存警告,我不知道擦除出了什么问题,它占用了这么多内存。

编辑:代码,已更新以显示静态分析器说它们是内存问题的位置

【问题讨论】:

  • 仪器是否通过 Leaks 仪器报告任何内存泄漏?您是否启用了僵尸(这将导致释放的内存实际上从未被释放)?您是否在 Xcode 中对您的代码运行了静态分析器以查看它是否报告了您的内存管理问题?
  • 你好@Gavin,它报告了CoreGraphics函数的内存泄漏,我不知道僵尸,我没有运行任何静态分析器
  • 我运行了静态分析器,它在上面的代码行中报告了问题,我已经更新了。
  • 你好@Gavin,关于这个你有什么要说的

标签: ios core-graphics cgcontext cgpath cglayer


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2015-01-27
    • 2021-06-04
    • 2016-11-09
    • 2021-06-21
    • 2012-05-11
    相关资源
    最近更新 更多