Swift Mapkit删除注释并释放内存

问题描述

我正在使用mapView.removeAnnotations(mapView.annotations)删除注释,但是从Xcode的调试中可以看到,每个注释的内存都没有释放,这最终导致我的应用崩溃。有没有办法释放它?我已经看过ARC以及deinit和weak,但是我看不到它与我的代码有什么关系。

import Foundation
import MapKit

class StationMarkerView: MKMarkerAnnotationView {
  override var annotation: MKAnnotation? {
    willSet {
        
      guard let station = newValue as? Station else {
        return
      }
      canShowCallout = true

        let mapsButton = FavouriteButton()
        let  bool = station.is_favourite
        let image = bool! ? "star.fill": "star"
        
        mapsButton.setBackgroundImage(UIImage(systemName: image),for: .normal)
        
        rightCalloutAccessoryView = mapsButton
        
      markerTintColor = station.markerTintColor
      glyphImage = station.glyphImage
      
    }
  }
}

我曾尝试对“ mapButton”使用弱函数,但Xcode给了我一个解除分配警告。

感谢任何帮助。

解决方法

我找到了解决问题的快速而肮脏的方法。删除注释,然后更改maptype似乎会释放与mapView相关的所有内存。

所以我要刷新地图...

mapView.removeAnnotations(mapView.annotations)
        
mapView.mapType = MKMapType.satellite
        
velibManager.fetchVelib()
        
mapView.mapType = MKMapType.standard