我遵循了几个SO问题中提供的建议,例如this one https://stackoverflow.com/questions/20138419/stop-ios-7-mkmapview-from-leaking-memory,为了释放MKMapView
从记忆中 - 我的代码如下
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.map.mapType = MKMapTypeHybrid;
self.map.showsUserLocation = NO;
self.map.delegate = nil;
[self.map removeFromSuperview];
self.map = nil;
self.locationManager.delegate = nil;
}
它在一定程度上有效,但并非完全有效。让我提供一些数据。
以下是 Instruments 的内存分配记录。
两个红旗(Generations)表示我显示之前的状态MKMapView
在模态视图控制器中以及在我关闭它之后。MKMapView
似乎被释放了。例如,如果我在仪器中过滤统计堆栈MKMapView
,该对象确实在模态视图呈现时出现,并在关闭后消失。然而,在关闭地图视图后,我仍然有 30+ MB 的内存尚未释放。
B 代(第二个危险信号)数据显示有大量对象(和非对象)持有该内存。
When I look at extended details of one of those instances, it usually shows a stack trace that features private classes that, I guess, are related to map drawing
有谁知道如何释放所有数据?我可以/应该清理一些缓存吗?
在我的应用程序中,它在选项卡视图控制器的控制下使用地图视图控制器,我存储了对MKMapView
在静态变量中并一遍又一遍地使用相同的地图视图,而不是在ViewDidLoad
每次。我的(部分)代码:
@implementation PubMapViewController {
NSMutableArray *annotations;
}
static MKMapView *_mapView = nil;
- (void)viewDidLoad {
[super viewDidLoad];
if (!_mapView) {
_mapView = [[MKMapView alloc] init]; // frame set up with constraints
} else {
[_mapView removeAnnotations:_mapView.annotations]; // precaution
}
[_mapViewProxy addSubview:_mapView];
[self addConstraints:_mapView];
[self configureView:((PubTabBarController *)self.tabBarController).detailItem];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_mapView addAnnotations:annotations];
if (annotations.count == 1) {
[_mapView selectAnnotation:annotations[0] animated:YES];
}
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_mapView removeAnnotations:_mapView.annotations];
}
Here, configureView:
设置地图self.tabBarController.detailItem
,设置其委托并将地图注释存储在变量中annotations
.
该地图成为界面构建器中定义的视图的子视图(实例变量@property (weak, nonatomic) IBOutlet UIView *mapViewProxy;
)。地图的大小必须与mapViewProxy
,当我使用自动布局时,框架大小为_mapView
完全使用中设置的约束进行控制addConstraints
(上、下、左、右等于_mapView.superview
).
我发现必须从地图中删除注释viewDidDisppear:
并将它们添加回viewDidAppear
。取消设置可能会更干净_mapView.delegate
in viewDidDisppear:
并将其设置回viewDidAppear
.
顺便说一句:静态变量_mapView
仍然误导性地带有前导下划线,因为在通过定义设置之前它是一个实例变量MKMapView
in IB.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)