问题描述
我正在使用自定义 MKOverlay / MKOverlayView
使用我自己的磁贴完全覆盖Google底图,这些磁贴是异步加载的。当我收到 canDrawMapRect:zoomScale:
调用我的覆盖视图(并在这种情况下返回FALSE),然后调用 setNeedsDisplayInMapRect:zoomScale:。
I'm using a custom MKOverlay/MKOverlayView
to completely cover the Google basemap with my own tiles, which are loaded asynchronously. I follow the pattern of requesting unloaded tiles when I receive a canDrawMapRect:zoomScale:
call to my overlay view (and returning FALSE in that case), then calling setNeedsDisplayInMapRect:zoomScale:
once the tile is available.
这一切通常都有效,并且似乎在模拟器中完美运行。
This all generally works, and appears to work perfectly in the simulator.
但是,在设备上我有时会在叠加层中找到一个洞 - 一块缺失的瓷砖。
However, on the device I sometimes get a 'hole' in the overlay - a missing tile.
我可以看到请求了磁贴,并且请求完成了。我可以看到我调用 setNeedsDisplayInMapRect:zoomScale:
,并且我正在传递原始 MKMapRect
和 MKZoomScale
在 canDrawMapRect:zoomScale:
中提供。但是我也可以看到覆盖层永远不会被要求重绘那个图块(既不是 canDrawMapRect:zoomScale:
也不是 drawMapRect:zoomScale:inContext:
再次被称为该牌。)
I can see that the tile is requested, and that the request completes. I can see that I call setNeedsDisplayInMapRect:zoomScale:
, and that I am passing the original MKMapRect
and MKZoomScale
which were provided in canDrawMapRect:zoomScale:
. But I can also see that the overlay is never asked to redraw that tile (neither canDrawMapRect:zoomScale:
nor drawMapRect:zoomScale:inContext:
is ever again called for that tile).
我需要了解为什么会发生这种情况以及如何纠正它。
I need to understand why this is happening and how to correct it.
以下是我的MKOverlayView子类中的相关代码:
Here's the relevant code from my MKOverlayView subclass:
- (BOOL) canDrawMapRect: (MKMapRect) mapRect zoomScale: (MKZoomScale) zoomScale
{
NSUInteger zoomLevel = [self zoomLevelForZoomScale:zoomScale];
CGPoint mercatorPoint = [self mercatorTileOriginForMapRect:mapRect];
NSUInteger tilex = floor(mercatorPoint.x * [self worldTileWidthForZoomLevel:zoomLevel]);
NSUInteger tiley = floor(mercatorPoint.y * [self worldTileWidthForZoomLevel:zoomLevel]);
NSURL* tileUrl = [self tileURLForZoomLevel: zoomLevel tileX: tilex tileY: tiley];
ASIHTTPRequest* tileRequest = [ASIHTTPRequest requestWithURL: tileUrl];
tileRequest.downloadCache = [ASIDownloadCache sharedCache];
[tileRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
if ( NO == [[ASIDownloadCache sharedCache] isCachedDataCurrentForRequest: tileRequest] )
{
[tileRequest setCachePolicy: ASIAskServerIfModifiedWhenStaleCachePolicy];
tileRequest.delegate = self;
tileRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[NSValue value: &mapRect withObjCType: @encode( MKMapRect )], @"mapRect",
[NSValue value: &zoomScale withObjCType: @encode( MKZoomScale )], @"zoomScale",
[NSNumber numberWithInt: tilex], @"tilex",
[NSNumber numberWithInt: tiley], @"tiley",
nil];
[_tileRequestStack addOperation: tileRequest];
NSLog( @"canDrawMapRect: %d, %d - REQUESTING", tilex, tiley );
return NO;
}
NSLog( @"canDrawMapRect: %d, %d - READY", tilex, tiley );
return YES;
}
- (void) drawMapRect: (MKMapRect) mapRect zoomScale: (MKZoomScale) zoomScale inContext: (CGContextRef) context
{
NSUInteger zoomLevel = [self zoomLevelForZoomScale:zoomScale];
CGPoint mercatorPoint = [self mercatorTileOriginForMapRect:mapRect];
NSUInteger tilex = floor(mercatorPoint.x * [self worldTileWidthForZoomLevel:zoomLevel]);
NSUInteger tiley = floor(mercatorPoint.y * [self worldTileWidthForZoomLevel:zoomLevel]);
NSLog( @"drawMapRect: %d, %d", tilex, tiley );
NSURL* tileUrl = [self tileURLForZoomLevel: zoomLevel tileX: tilex tileY: tiley];
NSData* tileData = [[ASIDownloadCache sharedCache] cachedResponseDataForURL: tileUrl];
UIGraphicsPushContext(context);
if ( tileData != nil )
{
UIImage* img = [UIImage imageWithData: tileData];
if ( img != nil )
{
[img drawInRect: [self rectForMapRect: mapRect] blendMode: kCGBlendModeNormal alpha: 1.0 ];
}
else
{
NSLog( @"oops - no image" );
}
CGSize s = CGContextConvertSizeToUserSpace( context, CGSizeMake( 40, 1 ));
UIFont* f = [UIFont systemFontOfSize: s.width];
[[UIColor blackColor] setFill];
[[NSString stringWithFormat: @"%d,%d", tilex, tiley] drawInRect: [self rectForMapRect: mapRect] withFont: f];
}
UIGraphicsPopContext();
}
- (void) requestFinished: (ASIHTTPRequest *) tileRequest
{
NSValue* mapRectValue = [tileRequest.userInfo objectForKey: @"mapRect"];
MKMapRect mapRect; [mapRectValue getValue: &mapRect];
NSValue *zoomScaleValue = [tileRequest.userInfo objectForKey:@"zoomScale"];
MKZoomScale zoomScale; [zoomScaleValue getValue: &zoomScale];
NSLog( @"requestFinished: %d, %d, %lf",
[[tileRequest.userInfo objectForKey:@"tilex"] intValue],
[[tileRequest.userInfo objectForKey:@"tiley"] intValue],
zoomScale );
[self setNeedsDisplayInMapRect: mapRect zoomScale: zoomScale];
}
编辑:
推荐答案
我遇到的问题与此处描述的问题非常相似。在我的情况下,我无法重现所需的行为(在 :)即使拥有最简单的代码:
I had an issue very similar to the one described here. In my case I couldn't reproduce the desired behaviour (described in http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKOverlayView_class/Reference/Reference.html#//apple_ref/occ/instm/MKOverlayView/setNeedsDisplayInMapRect:zoomScale:) even having the most simple code possible:
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
NSLog(@"This should trace forever");
[self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
return NO;
}
或更接近原始代码:
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
NSLog(@"This should trace forever");
[SomeAsynchronousRequestWithCompletionHandler:^{
[self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
}];
return NO;
}
在这两种情况下 setNeedsDisplayInMapRect:zoomScale:从未甚至被调过一次。
In both cases setNeedsDisplayInMapRect:zoomScale: has never been called even once.
当我开始在调度到canDrawMapRect运行的同一队列的dispatch_async中运行 setNeedsDisplayInMapRect:zoomScale:时情况发生了变化on,like:
The situation changed, when I began running setNeedsDisplayInMapRect:zoomScale: inside a dispatch_async dispatched to the same queue that canDrawMapRect runs on, like:
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
dispatch_queue_t queue = dispatch_get_current_queue();
NSLog(@"This should trace forever");
dispatch_async(queue, ^{
[self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
});
return NO;
}
或包含异步作业:
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
NSLog(@"This should trace forever");
dispatch_queue_t queue = dispatch_get_current_queue();
[SomeAsynchronousRequestWithCompletionHandler:^{
dispatch_async(queue, ^{
[self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
});
}];
return NO;
}
使用dispatch_async - 我可以看到这应该永远追踪字符串被无休止地追踪。我原来的问题也完全消失了。
Using dispatch_async - I can see "This should trace forever" string being traced endlessly. My original problem is also disappeared completely.
稍后更新:目前,我使用dispatch_get_main_queue()来调用 setNeedsDisplayInMapRect:zoomScale:,如
LATER UPDATE: Currently, I use dispatch_get_main_queue() to call setNeedsDisplayInMapRect:zoomScale: like
- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale {
NSLog(@"This should trace forever");
[SomeAsynchronousRequestWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self setNeedsDisplayInMapRect:mapRect zoomScale:zoomScale];
});
}];
return NO;
}
这篇关于setNeedsDisplayInMapRect不会触发新的drawMapRect:call的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!