2012-03-05 51 views
1

我使用下面的代碼在iPad上測試過。MKOverlayView性能問題。什麼也不做。但是緩慢

如果我像下面的代碼一樣添加MyOverlay,那麼即使在drawMapRect中什麼都不做,默認地圖圖塊的MKMapView加載速度會變慢。

如果我從MKMapView中刪除MyOverlay,默認地圖塊的加載會再次變快。

我不知道在調用drawMapRect時後臺做了什麼。

或下面的代碼有任何性能問題?

[代碼]

@implementation MyOverlay 

-(id) init 
{ 
    self = [super init]; 

    boundingMapRect = MKMapRectWorld; 
    boundingMapRect.origin.x += 1048600.0; 
    boundingMapRect.origin.y += 1048600.0; 

    coordinate = CLLocationCoordinate2DMake(0, 0); 

    return self; 
} 

... 
@end 


@implementation MyOverlayView 

- (id) initWithOverlay:(id<MKOverlay>)overlay 
{ 
    self = [super initWithOverlay:overlay]; 

    ... 
    return self; 
} 

- (BOOL) canDrawMapRect:(MKMapRect) sm zoomScale:(MKZoomScale)zoomScale 
{ 
    return true; 
} 

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext: CGContextRef)context 
{ 
    return; // do nothing, but map loading become slower 'much'. 
} 
@end 

回答

0

的MKOverlayView的繪製方法drawMapRect:zoomScale:inContext:的名字讓我想起了UIView的drawRect方法。而MKOverlayView是UIView的子類。的UIView的子類提供了這樣的警告:

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

如果這仍然是一個關注,你應該看一看這太問題,約drawRect的表現:to drawRect or not to drawRect (when should one use drawRect/Core Graphics vs subviews/images and why?)