swift - 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 给了我一个解除分配警告。
感谢任何帮助表示赞赏。
解决方案
我找到了一个快速而肮脏的解决方案来解决我的问题。删除注释然后更改地图类型似乎会释放与地图视图相关的所有内存。
所以要刷新我的地图,我会...
mapView.removeAnnotations(mapView.annotations)
mapView.mapType = MKMapType.satellite
velibManager.fetchVelib()
mapView.mapType = MKMapType.standard
推荐阅读
- javascript - SCRIPT1010:IE11 Vuejs Nuxt 中的预期标识符
- javascript - 如何使这个基本的 JS 引导表能够添加和删除
- java - 在 Spring Boot 中使用 Google OAuth2 时无法访问 OAuth2AuthenticationToken
- android - 复制&剪切&粘贴/自动完成在 Mac 上的 Android Studio 中无法正常工作
- c++ - 如何制作一个读取字符串数组中的信息然后将其分类到一个类中的 C++ 程序?
- java - 使用步骤 1、2 或 3 计算到达第 4 级楼梯的方法
- java - 在 Java 中接受来自外部文本文件的 SQL 查询
- c# - 如何获取列表
从 System.Object 通过反射检索? - android - 您的游戏错误地实现了以下 Play 游戏服务: 成就
- django - 如何为用户 OneToOneField url 自动生成 slug