2010-07-30 63 views
6

我正在研究一个iPhone应用程序,该应用程序在某些位置显示带有多个圆形覆盖图的地图。 我遇到严重的内存问题和崩溃,当我添加超过6个圈子,我缩小足够远,他们都可见。 当我放大以至只有2个圆圈可见时,一切都很好。当我删除MKOverlays时,一切正常。MKMapView上的多个MKOverlays导致内存警告

任何人认识到这种行为?

创建叠加层的代码。我存储在一个的NSMutableDictionary的叠加以供将来参考(能够从地图上删除,并避免双重叠加),使覆盖观点,即释放

#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)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]; 
     } 
    } 
} 

代码覆盖缓存

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

好奇这是发生在iOS版本。仪器在哪里看到内存消耗峰值? – Nick 2010-07-31 16:20:05

+0

我正在运行iOS 4.0。 MKCircle类是在4.0中添加的。 我做了一些更多的测试,它似乎只会导致iPhone 3G上的严重问题。 3GS和模拟器工作正常。 我在乐器中没有看到任何尖刺,所以很难调查此问题。 – rule 2010-08-01 13:05:42

回答

5

我看到相同的事情发生。我正在画MKPolylines而不是圈子,但我有完全相同的问题。 1行工作正常,但是当我开始添加几个,并尝试移动它周围的地图崩溃与内存警告。我会粘贴我的代码,但它与上述换行符相同。

编辑︰问题似乎是每个覆盖层创建一个新的核心动画层。有一种变通方法在这里 - https://devforums.apple.com/thread/48154?tstart=0另外,我相信这是一个已知的bug应固定在下一版本

编辑:变通办法 - “这是不符合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 
+0

谢谢,我会尽力解决此问题! – rule 2010-08-03 08:28:51

+1

这个错误是否在iOS 4.x版本中修复过? – 2011-05-22 00:38:22

+2

是的,他们固定在4.1 – clarky 2011-08-02 16:13:40