美文网首页
高德地图CPU飙升问题

高德地图CPU飙升问题

作者: 武个武6个武 | 来源:发表于2016-10-24 17:15 被阅读0次

    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飙升问题

          本文链接:https://www.haomeiwen.com/subject/aqpluttx.html