0

在我的 UIView 中,我有几个 CGPath 存储在 NSArray(availablePaths)中。

绘制任何内容之前,我创建每个 CGPath 的缩放版本并替换数组中的旧版本。

我使用以下代码在 drawRect 中获得 SIGABRT。

- (void) scaleAllPaths: (CGFloat) scaleFactor
{

 CGAffineTransform transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
 CFIndex i;
 for(i=0;i<[availablePaths count];i++){
    id obj = [availablePaths objectAtIndex:i];
    CGPathRef oldPath = (CGPathRef)obj;
    CGMutablePathRef newPath = CGPathCreateMutable();
    CGPathAddPath(newPath, &transform, oldPath);
    [availablePaths replaceObjectAtIndex:i withObject:(id) newPath];
    CGPathRelease(newPath); // <--- causes SIGABRT in drawRect:
 }
}
-(void)drawRect:(CGRect)rect
{
...
 for (int i = 0; i < currentPathIndex; i++) {
      id obj = [availablePaths objectAtIndex:i];
      CGPathRef path = (CGPathRef)obj;
      CGContextBeginPath(context);
      CGContextAddPath(context, path);    // <---  SIGABRT
      CGContextDrawPath(context, kCGPathEOFill);

 }
...

如果没有 CGPathRelease(newPath) 绘图工作正常,但当然,我得到了内存泄漏。

4

2 回答 2

1

发现,我正在另一个类中释放 CGPath 对象,我将它发送到:

-(void) setPath: (CGPathRef) pathToSet forceClosed: (BOOL) fClosed
{

  if(_path){
    CGPathRelease(_path);
  }
  _path = pathToSet;
  CGPathRetain(_path);  // <--- was missing
}

谢谢,R。

于 2013-03-16T14:15:17.137 回答
1

尝试在此处找到的解决方案,将CGPathRefs 添加到NSMutableArray: wrap CGPathRefinside UIBezierPath

编辑:看起来你可以-[UIBezierPath applyTransform:]用来避免CGPathRefscaleAllPaths:.

- (void) scaleAllPaths: (CGFloat) scaleFactor
{
  CGAffineTransform transform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
  for (UIBezierPath *bezierPath in availablePaths){
    [bezierPath applyTransform:transform];
  }
}

- (void)drawRect:(CGRect)rect
{
  ...
  for (int i = 0; i < currentPathIndex; i++) {
    UIBezierPath *bezierPath = [availablePaths objectAtIndex:i];
    CGPathRef path = bezierPath.CGPath;
    CGContextBeginPath(context);
    CGContextAddPath(context, path);
    CGContextDrawPath(context, kCGPathEOFill);
  }
  ...
}
于 2013-03-15T14:20:35.263 回答