MKMapView每次加载其视图时都会占用大量内存 [英] MKMapView using a lot of memory each time i load its view
问题描述
我有一个非常简单的应用程序,有几个视图控制器。第二个视图控制器中有一个MKMapView。它设置正确,功能正常。问题是,每次我加载它的视图时,内存使用量会跳跃~30mb,并且永远不会回落,所以每次进入视图时它都会继续跳跃并最终变得超高。
当我离开控制器时,我尝试删除地图视图,如下所示:
I have a pretty simple app, with a couple of view controllers. There is a MKMapView in the second view controller. It is set up correctly, and functions fine. The problem is, each time I load its view the Memory usage jumps ~30mb, and never goes back down, so each time i go into the view it keeps jumping and eventually gets super high. I tried removing the map view when i leave the controller like this:
override func viewWillDisappear(animated: Bool) {
map.removeFromSuperview()
}
但它没有对记忆有任何影响。地图视图委托设置为其视图控制器。
but it doesn't have any effect on the memory. The map views delegate is set to its view controller.
我尝试使用Xcode仪器检查泄漏但没有找到任何东西。
I tried checking for leaks using Xcode instruments but didn't find anything.
有谁知道如何解决这个问题?
Does anyone know how to fix this?
谢谢
编辑:
添加此功能似乎有效:
Adding this seems to work:
func removeNastyMapMemory() {
map.mapType = MKMapType.Hybrid
map.delegate = nil
map.removeFromSuperview()
map = nil
}
override func viewWillDisappear(animated: Bool) {
removeNastyMapMemory()
}
推荐答案
这不是Swift问题,来自Objective-C时代。处理此问题的可能方法取决于应用程序的情况和行为。
This is not Swift issue, is coming from Objective-C days. The possible ways to handle this issue is depending upon the situation and behavior of the app.
-
如果您正在使用地图多次(或多次),只创建一个(共享)实例。你可以随时使用它。
If you're using a Map for multiple times (or places), only create a single (shared) instance of it. Which you can use it whenever you want.
或者如果你只使用它一次,那么试试这里的解决方案,https://stackoverflow.com/a/25419783/1603234 。这可能有所帮助。减少一点。但不是全部。
Or If you're only using it for once, then try a solution from here, https://stackoverflow.com/a/25419783/1603234. This may help. Reduce little. But not all.
这篇关于MKMapView每次加载其视图时都会占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!