【问题标题】:Google Maps SDK causing leak iOS 7Google Maps SDK 导致 iOS 7 泄露
【发布时间】:2014-01-20 11:32:36
【问题描述】:

加载谷歌地图时在分析器中出现泄漏。我根据谷歌的示例代码创建了一个非常简单的视图控制器,我发现地图加载时出现泄漏。我相信泄漏是在 SDK 本身。有没有人遇到过这个问题并找到了解决方案?

基本视图控制器

//
//  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

【问题讨论】:

  • 我在我的应用程序中编写了相同的代码,每次我按下开关时,我的应用程序都会增加 50 MB。这是非常糟糕的:(你找到答案了吗?
  • 不,恐怕不行,我最后还是用了苹果地图。如果我遇到答案,我会告诉你。

标签: ios google-maps memory-leaks


【解决方案1】:

我找到了问题的原因。像你一样,我认为他们的“演示代码”应该按原样工作。不明白这一行

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:6];

演示中的第一行实际上就是问题所在。 如果您不在澳大利亚(此位置点所在的位置) 您导致将整个世界地图(图块)加载到应用程序的内存中,这是错误的!

如果您大致了解您的地图将用于哪个洲/国家/州 或者更好!在显示地图之前获取用户的位置,然后在该位置加载地图。

所以你的地图初始化应该是这样的:

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;
    });

}

缩放级别对此也有影响 -> 缩放越大,加载到内存中的图块越少。

我还在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 之间时。

【讨论】:

  • 您演示的viewWillDisappear 显着减少了我的内存使用量。谢谢
猜你喜欢
  • 2019-02-14
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 2015-12-17
相关资源
最近更新 更多