高德地图CPU飙升问题

1.在退出的时候及时释放代理 viewwilldisappear的时候,delegate设置为nil

 - (void)viewWillDisappear:(BOOL)animated {

  [MAMapView shareMAMapView].showsUserLocation = NO;
  [MAMapView shareMAMapView].delegate = nil;

  [super viewWillDisappear:animated];
}

2.每次进入地图都会new一个mapview出来。进进出出,cpu飙升,用单例实现地图

static MAMapView *_mapView = nil;

+ (MAMapView *)shareMAMapView {
  @synchronized(self) {
    [MAMapServices sharedServices].apiKey = kAMapSearchApplicationSecretKey;

    if (_mapView == nil) {
      CGRect frame = [[UIScreen mainScreen] bounds];
      _mapView = [[MAMapView alloc] initWithFrame:frame];
      _mapView.autoresizingMask =
          UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
      _mapView.showsUserLocation = YES;
      //      _mapView.rotateEnabled = YES;
      //      _mapView.rotateCameraEnabled = YES;
      _mapView.zoomEnabled = YES;
    }
    _mapView.frame = [UIScreen mainScreen].bounds;
    return _mapView;
  }
}


//重写allocWithZone保证分配内存alloc相同
+ (id)allocWithZone:(NSZone *)zone {
  @synchronized(self) {

    if (_mapView == nil) {
      _mapView = [super allocWithZone:zone];
      return _mapView; // assignment and return on first allocation
    }
  }
  return nil; // on subsequent allocation attempts return nil
}

//保证copy相同
+ (id)copyWithZone:(NSZone *)zone {
  return _mapView;
}

你可能感兴趣的:(高德地图CPU飙升问题)