我正在编写一个 iPhone 应用程序,其中我使用 MKOverlayView 在 MKMapView 上放置一个大的 PNG 图像(1936 × 2967)。我对如何在 MKOverlayView 中适当地实现 drawMapRect: 函数有点困惑——我应该在绘制图像之前手动分割我的图像吗?或者我应该让 MKOverlayView 的机制来处理所有这些吗?
我从其他帖子中得到的印象是,在 MKOverlayView 可用之前,您应该为此类任务自己分割图像,并使用 CATiledLayer。我想也许 MKOverlayView 处理了所有的脏活。
我问的真正原因是,当我使用分配工具通过 Instruments 运行我的应用程序时,我发现我的应用程序使用的实时字节数随着地图上自定义图像的引入而稳步增加。现在我没有分割我的图像,但我也没有在 Instruments 的泄漏工具中看到内存泄漏的记录。这是我的 drawMapRect: 函数:
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
// Load image from applicaiton bundle
NSString* imageFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"map.png"];
CGDataProviderRef provider = CGDataProviderCreateWithFilename([imageFileName UTF8String]);
CGImageRef image = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
MKMapRect overlayMapRect = [self.overlay boundingMapRect];
CGRect overlayRect = [self rectForMapRect:overlayMapRect];
// draw image
CGContextSaveGState(context);
CGContextDrawImage(context, overlayRect, image);
CGContextRestoreGState(context);
CGImageRelease(image);
}
如果我的 drawMapRect: 函数不是这些内存问题的原因,有人知道它可能是什么吗?我通过调试知道我的 mapView 的 viewForOverlay: 函数只为每个叠加层调用一次,所以并不是内存泄漏或其他原因。
欢迎任何建议!
谢谢,-马特
编辑:所以事实证明内存问题实际上是由 MKMapView 引起的 - 每次我移动地图时,内存使用量都会非常稳定地上升并且永远不会下降 - 这似乎不太好:(