【问题标题】:GMSMapView clear doesn't clear memoryGMSMapView clear 不会清除内存
【发布时间】:2017-08-03 20:46:11
【问题描述】:

我的应用中有一个 GMSMapView。带有打开地图屏幕的应用程序占用 150 mb 内存。

有时我会添加很多多边形。之后,应用程序需要 230 mb。

调用方法 [GMSMapView clear] 后,所有多边形都消失了,但应用程序仍占用 230 mb 内存。

指向多边形的指针不存储在其他任何地方。 如何让map清空内存以及为什么调用'clear'方法后没有发生?

【问题讨论】:

  • 是哪个版本的?
  • 最新的。 2.4.
  • 当我升级到 2.3 时,我遇到了一些问题。 2.2 版中是否也会发生这种情况?

标签: ios objective-c google-maps google-maps-sdk-ios gmsmapview


【解决方案1】:

嗯,这很痛苦......我没有找到官方文档。

我的解决方案:

Swift 3.0

1)不要使用Storyboard,在代码中创建mapView:

@IBOutlet weak fileprivate var mapViewContainer: UIView!

weak fileprivate var mapView: GMSMapView? {
    didSet { 
        //you can config and customise your map here
        self.configureMapView() 
    }
}

//If you use clustering
fileprivate var clusterManager: ClusterManager?
fileprivate var clusterRenderer: ClusterRenderer?

2) 创建 viewController 配置函数并在 viewDidLoad 之前调用它:

func configure() {
    mapView = GMSMapView()
    mapView?.delegate = self
}

3) 布局并将 mapView 添加到您的 UI:

override func viewDidLoad() {
    super.viewDidLoad()

    if let mapView = mapView {
        mapViewContainer.addSubview(mapView)
        //Create constraints as you wish
        mapView.fillView()
        mapViewContainer.layoutSubviews()
    }
}

4) 现在创建函数:

func releaseMemory() {

    if
        let mapView = mapView,
        let clusterManager = clusterManager {

        clusterManager.clearItems()
        mapView.clear()
        mapView.delegate = nil
        mapView.removeFromSuperview()

    }

    clusterManager = nil
    clusterRenderer = nil
    mapView = nil
}

你在ViewDidDisappear中调用它,或者调用delegate...

releaseMemory()函数清空内存,不是很聪明,但确实有效。

【讨论】:

  • 你能分享一个示例项目的github链接吗? ,也许它会帮助某人
【解决方案2】:

您可以尝试将多边形对象存储在某处,然后在所有对象上调用polygon.map = nil,删除多边形引用,然后调用地图视图clear

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多