因业务需求需要用到地图,开发完成后发现每次进入地图展示的页面内存都会增加,虽然dealloc后也会释放一些内存,但那也只是释放一小部分,用了Xcode检查内存工具排查也不存在代码上的问题,经过一些测试最后还是定位在了MKMapView的问题。
我的解决方案是在处理位置信息的单例里添加一个MKMapView的属性,需要的时候就创建它,用不到时就销毁,最后内存释放的结果也达到了满意的效果。
贴一下处理的代码:
self.mapView.showsUserLocation = NO;
self.mapView.userTrackingMode = MKUserTrackingModeNone;
[self.mapView.layer removeAllAnimations];
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView removeOverlays:self.mapView.overlays];
[self.mapView removeFromSuperview];
self.mapView.delegate = nil;
self.mapView = nil;