加载谷歌地图时在分析器中获取泄漏。我根据谷歌的示例代码创建了一个非常简单的 View Controller ,我发现我在加载 map 时遇到了泄漏。我相信泄漏是在 SDK 本身。有没有人遇到过这个问题并找到了解决方案?
基本 View Controller
//
// JRCViewController.m
// GoogleMapsInterface
//
// Created by Jake Cunningham on 15/01/2014.
// Copyright (c) 2014 Jake Cunningham. All rights reserved.
//
#import "JRCViewController.h"
@interface JRCViewController (){
BOOL firstLocationUpdate_;
GMSMapView *mapView;
}
@end
@implementation JRCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:6];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView;
dispatch_async(dispatch_get_main_queue(), ^{
mapView.myLocationEnabled = YES;
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (!firstLocationUpdate_) {
// If the first location update has not yet been recieved, then jump to that
// location.
firstLocationUpdate_ = YES;
CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
zoom:14];
}
}
@end
最佳答案
我找到了问题的原因。像你一样,我认为他们的“演示代码”应该按原样工作。不明白这一行
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:6];
演示中的第一行实际上就是问题所在。 如果你不在澳大利亚(这是这个位置点所在的地方) 您导致将整个世界地图(图 block )加载到应用程序的内存中,这是错误的!
如果您大致知道您的 map 将被用于哪个大陆/国家/州 或者更好!在显示 map 之前获取用户的位置,并在该位置加载 map 。
所以你对 map 的初始化应该是这样的:
CLLocation *location = [self getUserLocation];//可能来自共享首选项,即使它距离用户实际所在位置 100 英里,也比加载另一个大陆要好。
然后你的 viewDidLoad 看起来像这样
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocation *location = [self getUserLocation]; //<== very important!
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
longitude:location.coordinate.longitude
zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
self.view = mapView;
dispatch_async(dispatch_get_main_queue(), ^{
mapView.myLocationEnabled = YES;
});
}
缩放级别对此也有影响 -> 缩放越大,加载到内存中的图 block 越少。
我还在 viewWillDisappear
中添加了代码(假设当再次需要给定的 ViewController 时 viewDidLoad
将再次运行)
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.mapView clear];
[self.mapView removeFromSuperview] ;
self.mapView.delegate = nil;
self.mapView = nil ;
}
这帮助我的应用程序从必须使用 140 MB 的内存减少到仅 56! 当应用正常值介于 40 和 45 之间时。
关于ios - Google Maps SDK 导致 iOS 7 泄露,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21232913/