我正在开发一个基于 map 的应用程序。我用了MKTileOverlay
显示一些动态内容。有时,当新信息到达时,某些图 block 需要重新加载。
我尝试调用overlayRenderer.setNeedsDisplayInMapRect(mapRect)
在主线程中,但显然在我的情况下它不会触发 map 重绘。到目前为止,唯一有效的方法是 overlayRenderer.reloadData()
。但是,这将导致重新加载整个内容(而不是仅特定区域)并导致 View 闪烁,因此这不适合我。
有人可以就此提供任何建议吗?谢谢并干杯。
最佳答案
我终于明白了。事实上,叠加层未更新并不是由于 setNeedsDisplayInMapRect
造成的。经过几次检查后,我发现 setNeedsDisplayInMapRect 实际上导致了 DrawMapRect:zoomScale:inContext: 的运行,但是,默认重绘会产生相同的图 block 图像。我猜这可能是由于 MKTileOverlayRender 的一些内部缓存所致。
对我来说,解决方案是对 MKTileOverlayRender
进行子类化,并在新类中:
override func canDrawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale) -> Bool {
let tilePath = self.tilePathForMapRect(mapRect, andZoomScale: zoomScale)
let tilePathString = stringForTilePath(tilePath)
if let _ = self.cache.objectForKey(tilePathString) {
return true
} else {
let tileOverlay = self.overlay as! MKTileOverlay
tileOverlay.loadTileAtPath(tilePath, result: {
data, error in
if error == nil && data != nil {
if let image = UIImage(data: data!) {
self.cache.setObject(image, forKey: tilePathString)
self.setNeedsDisplayInMapRect(mapRect, zoomScale: zoomScale)
}
}
})
return false
}
}
override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext) {
let tilePath = self.tilePathForMapRect(mapRect, andZoomScale: zoomScale)
let tilePathString = stringForTilePath(tilePath)
if let image = self.cache.objectForKey(tilePathString) as? UIImage {
CGContextDrawImage(context, self.rectForMapRect(mapRect), image.CGImage)
} else {
super.setNeedsDisplayInMapRect(mapRect, zoomScale: zoomScale)
}
self.cache.removeObjectForKey(tilePathString)
}
关于ios - MKTileOverlay:如何重新加载特定图 block 或区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081372/