在iOS中加载数千个数据和放大时间时,MapView会冻结几秒钟
我正在用快速的语言开发Mapview
。 因为,我正在将geojson文件加载到我的mapview中,它的加载成功。 我正在加载GEOSwift
库。 它的工作正常,但是,发生了什么,在加载数据时,mapview在UI中冻结/停留几秒钟,然后显示数据。 即使在缩放时间内也会冻结UI几秒钟。
我正在加载LineString
类型数据,坐标大约为7000个数据。
即使我只在后台线程中解析数据。
我有两个3个选项卡,用于在我的地图中使用3 geojson
加载不同的数据
文件。 即使在将一个数据切换到另一个数据时,它也会冻结。
在调试时,在放大/缩小mapview时跟随错误打印。
[VKDefault] Tile 11720.7599.14 (128) in current unloaded state for 0.01 seconds - Raster Overlays Above Labels - Failed to decode (terminal) (0.01 sec)
我的代码如下。
@IBAction func mapDataAction(_ sender: Any) { let overlays = mapView.overlays mapView.removeOverlays(overlays) self.mapView.removeAnnotations(mapView.annotations) DispatchQueue.global(qos: .background).async { if let geoJSONURL = Bundle.main.url(forResource: “LineString”, withExtension: "geojson") { do { var overlays = [MKPolyline]() let features = try Features.fromGeoJSON(geoJSONURL) for item in features! { if let item = item.geometries![0] as? LineString { let polyLine = item.mapShape() as! MKPolyline overlays.append(polyLine) } } DispatchQueue.main.async { // add overlays to map self.mapView.addOverlays(overlays) } } catch { //handle error } } } }
并且一次性应用程序在控制台中抛出以下消息而崩溃。
Terminated due to memory issue
它从设备的CPU中获取了巨大的内存。
在滚动mapview或zoomin / zoomout时,它的调用始终是委托方法,这会冻结mapview。
public func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if let polyline = overlay as? MKPolyline { let renderer = MKPolylineRenderer(polyline: polyline) //renderer.fillColor = UIColor.blue.withAlphaComponent(0.5) renderer.strokeColor = UIColor.blue.withAlphaComponent(0.8) renderer.lineWidth = 5 print("MKPolylineRenderer delegate calling") return renderer } return MKOverlayRenderer(overlay: overlay) // fatalError("Unexpected overlay type") }
我怎么解决这个问题?