问题描述
我注意到我的应用程序泄漏了内存,但如果我拿出 MKMapView
,内存问题就会消失。
I've noticed that my app leaks memory, but if I take the MKMapView
out the memory problem goes away.
为了测试这个理论,我创建了一个简单的项目,它有一个视图,用 MKMapView
推送视图在它和弹出和推动。而已。视图控制器中没有代码,通过故事板完成。
To test the theory, I made a dead simple project that has a view that pushes a view with a MKMapView
in it and pops and pushes. Nothing more. No code in the view controllers, everthing done via storyboard.
如果我来回地图视图,在推送和弹出视图后,它开始大约3MB它中的地图约15倍,内存约为230MB。
If I go back and forth to the map view, it starts about 3MB after doing pushing and popping the view with the map in it this about 15 times the memory is around 230MB.
其他人看过这个?看起来像一个非常大的bug。有没有不同的方法来使用 MKMapView
来防止它泄漏这么多?
Anyone else seen this? Seems like a pretty big bug. Is there a different way to use MKMapView
that will prevent it from leaking so much?
推荐答案
我遇到了同样的问题,并且(感谢Stackoverflow)通过在 viewWillDisappear
中更改 MKMapType
并解除分配/设置它来修复它委托给nil。因为它仍然向代表发送消息。
这在MKMapViewDelegate协议参考中有记录:
I had faced the same issue and (thanks to Stackoverflow) fixed it by changing MKMapType
in viewWillDisappear
and deallocating/setting its delegate to nil.As it still sends message to delegates.This is documented in MKMapViewDelegate Protocol Reference:
。
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self applyMapViewMemoryFix];
}
- (void)applyMapViewMemoryFix{
switch (self.mkMapView.mapType) {
case MKMapTypeHybrid:
{
self.mkMapView.mapType = MKMapTypeStandard;
}
break;
case MKMapTypeStandard:
{
self.mkMapView.mapType = MKMapTypeHybrid;
}
break;
default:
break;
}
self.mkMapView.showsUserLocation = NO;
self.mkMapView.delegate = nil;
[self.mkMapView removeFromSuperview];
self.mkMapView = nil;
}
希望这会有所帮助
这篇关于阻止iOS 7 MKMapView泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!