MKMapView上的多个MKOverlay会导致内存警告

时间:2010-07-30 09:44:54

标签: iphone memory-management mapkit mkmapview

我正在开发一款iPhone应用,可以在某些位置显示包含多个圆形叠加层的地图。 当我添加超过6个圆圈时,我遇到严重的内存问题和崩溃,并且我缩小到足以让它们全部可见。 当我放大以便只能看到2个圆圈时,一切都很好。当我删除MKOverlays时,一切正常。

是否有人认识到这种行为?

创建叠加层的代码。我将叠加层存储在NSMutableDictionary中以供将来参考(能够从地图中删除它们并防止双重叠加)

- (void)updateMarkersForZones:(NSArray *)zones {
    NSLog(@"MapViewController: Update Markers");
    // For each zone, show a marker
    for (Zone* zone in zones) {
        NSString *keyMarker = [NSString stringWithFormat:@"%d-marker", zone.id];

        MKCircle *circle = [overlayCache objectForKey:keyMarker];
        if (circle == nil) {
            // draw the radius circle for the marker
            double radius = MAX(zone.markerRadius * 1.0, 1.0);
            circle = [MKCircle circleWithCenterCoordinate:zone.location radius:radius];
            [mapView addOverlay:circle];
            // store the circle in a cache for future reference
            [overlayCache setObject:circle forKey:keyMarker];
        }
    }
}

制作叠加视图的代码

#pragma mark -
#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
     MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:overlay] autorelease];
    circleView.lineWidth = 1.0;
    circleView.strokeColor = [UIColor redColor];
    return circleView;
}

释放重叠缓存的代码

- (void)dealloc {
    [overlayCache release];
    [mapView release];
    [super dealloc];
}

1 个答案:

答案 0 :(得分:6)

我看到同样的事情发生了。我正在绘制MKPolylines而不是圆圈,但我有完全相同的问题。 1行工作正常,但当我开始添加几个并尝试移动地图时,它会因内存警告而崩溃。我会粘贴我的代码,但它几乎与上面的行更改相同。

编辑:问题似乎是每个叠加层都会创建一个新的核心动画层。这里有一个解决方法 - https://devforums.apple.com/thread/48154?tstart=0另外,我相信这是一个已知的错误,应该在下一个版本中修复

编辑:解决方法 - “它不是API的问题,而是实现。我建议手动将它们合并为一个是暂时的解决方法。

例如,以下是如何实现MultiPolygon和相应的视图:“

    @interface MultiPolygon : NSObject <MKOverlay> {
    NSArray *_polygons;
    MKMapRect _boundingMapRect;
}

- (id)initWithPolygons:(NSArray *)polygons;
@property (nonatomic, readonly) NSArray *polygons;

@end

@implementation MultiPolygon

@synthesize polygons = _polygons;

- (id)initWithPolygons:(NSArray *)polygons
{
    if (self = [super init]) {
        _polygons = [polygons copy];

        NSUInteger polyCount = [_polygons count];
        if (polyCount) {
            _boundingMapRect = [[_polygons objectAtIndex:0] boundingMapRect];
            NSUInteger i;
            for (i = 1; i < polyCount; i++) {
                _boundingMapRect = MKMapRectUnion(_boundingMapRect, [[_polygons objectAtIndex:i] boundingMapRect]);
            }
        }
    }
    return self;
}

- (void)dealloc
{
    [_polygons release];
    [super dealloc];
}

- (MKMapRect)boundingMapRect
{
    return _boundingMapRect;
}

- (CLLocationCoordinate2D)coordinate
{
    return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(_boundingMapRect), MKMapRectGetMidY(_boundingMapRect)));
}

@end



@implementation MultiPolygonView

- (CGPathRef)polyPath:(MKPolygon *)polygon
{
    MKMapPoint *points = [polygon points];
    NSUInteger pointCount = [polygon pointCount];
    NSUInteger i;

    if (pointCount < 3)
        return NULL;

    CGMutablePathRef path = CGPathCreateMutable();

    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) {
        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);
            CGContextStrokePath(context);
            CGPathRelease(path);
        }
    }
}

@end