2010-07-30 41 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