【发布时间】:2023-03-07 14:47:01
【问题描述】:
我正在开发一个 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];
}
【问题讨论】:
-
好奇这是在哪个版本的 iOS 上发生的。您在哪里看到仪器的内存消耗激增?
-
我正在运行 iOS 4.0。 MKCircle 类是在 4.0 中添加的。我做了一些更多的测试,它似乎只会导致 iPhone 3G 出现严重问题。 3GS 和模拟器工作正常。我没有在仪器中看到任何尖峰,这使得调查变得困难..
标签: iphone memory-management mapkit mkmapview