objective c mkmapview重叠内存警告

时间:2014-03-10 13:03:39

标签: ios iphone objective-c mkmapview

我正在开发一个显示MKMapView的ios公园指南应用程序。为了展示我们自己的地图,我们设计了一个大尺寸(2499x1504px)的jpg叠加图像。 MKMapView还显示视图注释。

我在mapview周围扫描并检查调试导航器中的内存大小,并识别(有时,并非总是)内存增长到100-180 mb。 如果内存大约是150 + mb,我会收到内存警告,应用程序因内存问题而崩溃。 在模拟器中,应用程序永远不会崩溃。

为了测试目的,我删除了所有叠加层和注释并再次滑动。在某些情况下,内存也会增长到100-120mb而没有内存警告。

我的设备:iPhone 4s

实施:

if ([overlay isKindOfClass:[LGSOverlay class]]) {
        UIImage *theImage = [UIImage imageNamed:@"overlayImage"];
        XYZOverlayRenderer *overlayRenderer = [[XYZOverlayRenderer alloc] initWithOverlay:overlay overlayImage:theImage];
        return overlayRenderer;
}

XYZOverlayRenderer:

-(id)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
    self = [super initWithOverlay:overlay];
    if (self) {
        _overlayImage = overlayImage;
    }
    return self;
}

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGImageRef imageReferenc = self.overlayImage.CGImage;    

    MKMapRect theMapRect = self.overlay.boundingMapRect;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextDrawImage(context, theRect, imageReferenc);
}

什么是解决此问题的最佳解决方案?

修改

我按照本教程解决了这个问题: http://www.shawngrimes.me/2010/12/mapkit-overlays-session-1-overlay-map/

1 个答案:

答案 0 :(得分:0)

模拟器永远不会耗尽内存,所以不要觉得奇怪。转换为视频缓冲区后,该大小的JPEG图像太大了。 iPhone 4s有512MB的RAM,由GPU和CPU共享。您可能必须找到平铺图像的方法或使图像更小。

你怎么知道崩溃是由于内存问题?你得到一些内存警告,然后他的应用程序被终止?也许还有另一个问题。

您可能还会考虑使用更原生的压缩PowerVR格式,而不是使用jpeg。但是在OpenGL ES之外,我不确定它们是如何得到支持的。

请注意,在iOS 6+下,Mapkit使用向量,有时需要80-100 MB才能显示地图数据。