GMSMapView清除不清除内存

时间:2017-08-03 20:46:11

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

我的应用程序中有一个GMSMapView。具有开放式地图屏幕的应用程序在内存中需要150 MB。

在某些时候我添加了很多多边形。之后应用程序需要230 MB。

调用方法[GMSMapView clear]后,所有多边形都会消失,但app仍然需要230 MB的内存。

指向多边形的指针不会存储在任何其他位置。 如何让地图清除记忆,为什么在调用'clear'方法后不会发生这种情况?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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()功能清晰记忆,不那么聪明,但它有效。

相关问题