gpt4 book ai didi

iphone - 正确使用MKOverlayView

转载 作者:行者123 更新时间:2023-12-03 21:21:29 25 4
gpt4 key购买 nike

我正在编写一个 iPhone 应用程序,其中使用 MKOverlayView 将一个大的 PNG 图像 (1936 × 2967) 放置在 MKMapView 上。我对如何在 MKOverlayView 中正确实现 drawMapRect: 函数有点困惑 - 我应该在绘制图像之前手动分割图像吗?或者我应该让 MKOverlayView 的机制来处理所有这些?

我对其他帖子的印象是,在 MKOverlayView 可用之前,您需要自己为此类任务分割图像,并使用 CATiledLayer。我想也许 MKOverlayView 处理了所有肮脏的工作。

我问的真正原因是,当我使用分配工具通过 Instruments 运行我的应用程序时,我发现随着 map 上自定义图像的引入,我的应用程序使用的实时字节数稳步增加。现在我没有分割我的图像,但我在 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:函数不是这些内存问题的原因,有人知道它可能是什么吗?通过调试我知道我的 viewForOverlay: mapView 函数只会为每个覆盖层调用一次,因此并不是内存泄漏或其他原因。

欢迎任何建议!

谢谢,-马特

编辑:所以事实证明内存问题实际上是由 MKMapView 引起的 - 每次我移动 map 时,内存使用量都会非常稳定地上升并且永远不会下降 - 这看起来不太好:(

最佳答案

回答有点晚了,如果将来其他人遇到同样的问题,请将其留在这里。这里的缺陷是试图渲染整个图像,而文档明确指出 -

In addition, you should avoid drawing the entire contents of the overlay each time this method is called. Instead, always take the mapRect parameter into consideration and avoid drawing content outside that rectangle.

所以,你只需在mapRect定义的区域中绘制图像的部分

更新:请记住,此处的绘制矩形可能比映射矩形大,需要相应地调整绘制和剪切区域

let overlayMapRect = overlay.boundingMapRect
let overlayDrawRect = self.rect(for: overlayMapRect)

// watch out for draw rect adjustment here --
let drawRect = self.rect(for: mapRect).intersection(overlayDrawRect)
let scaleX = CGFloat(image.width) / overlayRect.width
let scaleY = CGFloat(image.height) / overlayRect.height
let transform = CGAffineTransform.init(scaleX: scaleX, y: scaleY)
let imageCut = drawRect.applying(transform)

// omitting optionals checks, you should not
let cutImage = image.cropping(to: imageCut)

// the usual vertical flip issue with image.draw
context.translateBy(x: 0, y: drawRect.maxY + drawRect.origin.y)
context.scaleBy(x: 1, y: -1)
context.draw(image, in: drawRect, byTiling: false)

关于iphone - 正确使用MKOverlayView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4175369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com