4

我已经在地图中渲染了近 1000 个多边形。我得到多边形的路径使用

-   (CGPathRef)polyPath:(MKPolygon *)polygon
{
     MKMapPoint *points = [polygon points];
     NSUInteger pointCount = [polygon pointCount];
     NSUInteger i;
     if (pointCount < 3)
         return NULL;
     CGMutablePathRef path = CGPathCreateMutable();
     if([polygon isKindOfClass:[MKPolygon class]])
     {
            for (MKPolygon *interiorPolygon in polygon.interiorPolygons)
      {
       CGPathRef interiorPath = [self polyPath:interiorPolygon];
       CGPathAddPath(path, NULL, interiorPath);
       CGPathRelease(interiorPath);
       }
  }
     CGPoint relativePoint = [self pointForMapPoint:points[0]];
     CGPathMoveToPoint(path, NULL, relativePoint.x, relativePoint.y);
     for (i = 1; i < pointCount; i++) 
     {
            relativePoint = [self pointForMapPoint:points[i]];
            CGPathAddLineToPoint(path, NULL, relativePoint.x, relativePoint.y);
     }
     return path;
}

- (void)drawMapRect:(MKMapRect)mapRect
      zoomScale:(MKZoomScale)zoomScale
      inContext:(CGContextRef)context
{
    MultiPolygon *multiPolygon = (MultiPolygon *)self.overlay;
for (MKPolygon *polygon in multiPolygon.polygons) 
{
    if([polygon isKindOfClass:[MKPolygon class]])
    {
            CGPathRef path = [self polyPath:polygon];
            if (path) 
            {
                [self applyFillPropertiesToContext:context atZoomScale:zoomScale];
                CGContextBeginPath(context);
                CGContextAddPath(context, path);
                CGContextDrawPath(context, kCGPathEOFill);
                [self applyStrokePropertiesToContext:context atZoomScale:zoomScale];
                CGContextBeginPath(context);
                CGContextAddPath(context, path);
                CGContextSetAlpha(context,1.0);
                CGContextStrokePath(context);
            }
            CGPathRelease(path);
    }
}
}

我漏水了

CGPathRelease(interiorPath);

return path;

我知道我必须使用 CGPathRelease 释放路径,但是当我必须返回时在哪里释放它。

两者都泄漏了巨大的内存。我已经为此工作了几天,请帮助。

提前致谢

4

3 回答 3

8

您应该重命名您的方法,以-createPolyPath:明确它正在返回一个需要释放的核心基础对象,然后在您调用的代码中-createPolyPath:,您需要像这样释放它:

CGPathRef path = [someObjectOrClass createPolyPath:somePolygon];
// Do some stuff with the path
CGPathRelease(path);

请参阅《Core Foundation 的内存管理编程指南》

于 2010-11-13T11:30:52.567 回答
1

我认为您必须重命名以newlike开头的方法newPolyPath...。我会做到的,它现在对我有用,路径上不再有泄漏......

您还必须CGPathRelease(path);在每次使用路径后使用。

于 2011-04-27T13:14:20.550 回答
1

尝试使用 CGPathRelease(path);

例如:

CGMutablePathRef path = CGPathCreateMutable(); // created memory allocation
CGPathCloseSubpath(path);
CGPathRelease(path); // released path allocation

在这里找到了很棒的提示:

http://the.ichibod.com/kiji/ios-memory-management-tips/

于 2015-02-23T04:00:43.950 回答