2010-07-30 46 views
6

Tôi đang làm việc trên ứng dụng iPhone hiển thị bản đồ có nhiều lớp phủ vòng tròn ở một số vị trí nhất định. Tôi đang gặp sự cố nghiêm trọng về bộ nhớ và sự cố khi tôi thêm hơn 6 vòng kết nối và tôi thu nhỏ đủ xa để tất cả chúng đều hiển thị. Khi tôi phóng to để chỉ hiển thị 2 vòng tròn, tất cả đều ổn. Khi tôi loại bỏ các MKOverlays, mọi thứ hoạt động tốt.Nhiều MKOverlays trên MKMapView dẫn đến cảnh báo bộ nhớ

Bất kỳ ai nhận ra hành vi này?

Mã tạo lớp phủ. Tôi lưu trữ các lớp phủ trong một NSMutableDictionary để tham khảo trong tương lai (để có thể loại bỏ chúng khỏi bản đồ và để ngăn chặn lớp phủ kép)

- (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]; 
     } 
    } 
} 

Mã mà làm cho các quan điểm overlay

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

Mã mà nhả overlay cache

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

Tò mò những gì phiên bản của iOS này đang xảy ra trên. Bạn thấy mức tăng tiêu thụ bộ nhớ trong các công cụ ở đâu? – Nick

+0

Tôi đang chạy iOS 4.0. Lớp MKCircle đã được thêm vào 4.0. Tôi đã làm một số thử nghiệm nhiều hơn và nó chỉ có vẻ dẫn đến các vấn đề nghiêm trọng trên iPhone 3G. 3GS và mô phỏng hoạt động tốt. Tôi không thấy bất kỳ sự đột biến nào trong các công cụ, điều này gây khó khăn cho việc điều tra điều này .. – rule

Trả lời

5

Tôi thấy điều tương tự cũng xảy ra. Tôi vẽ MKPolylines thay vì vòng tròn, nhưng tôi có chính xác cùng một vấn đề. 1 dòng hoạt động tốt, nhưng khi tôi bắt đầu để thêm một số và cố gắng di chuyển bản đồ xung quanh nó treo với cảnh báo bộ nhớ. Tôi muốn dán mã của mình, nhưng nó khá giống với vòng tròn thay đổi ở trên cho dòng.

CHỈNH SỬA: vấn đề có vẻ là mỗi lớp phủ tạo ra lớp hoạt ảnh lõi mới. Có một giải pháp khác ở đây - https://devforums.apple.com/thread/48154?tstart=0 Ngoài ra, tôi tin rằng đây là lỗi đã biết cần được khắc phục trong bản phát hành tiếp theo

EDIT: giải pháp thay thế - "Đây không phải là vấn đề với API mà đúng hơn là triển khai. tự củng cố chúng thành một là một cách giải quyết cho thời điểm này

Ví dụ, đây là cách bạn có thể thực hiện một MultiPolygon và xem tương ứng:."

@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

Cảm ơn, tôi sẽ thử công việc đó! – rule

+1

Lỗi này đã từng được khắc phục trong bản phát hành iOS 4.x chưa? –

+2

Có, họ đã sửa nó trong 4.1 – clarky

Các vấn đề liên quan