8

我正在使用自定义MKOverlay/MKOverlayView完全覆盖谷歌底图,我自己的瓷砖是异步加载的。canDrawMapRect:zoomScale:当我收到对我的覆盖视图的调用(并在这种情况下返回 FALSE)时,我遵循请求卸载的图块的模式,然后setNeedsDisplayInMapRect:zoomScale:在图块可用时调用。

这一切通常都有效,并且似乎在模拟器中完美运行。

然而,在设备上,我有时会在覆盖层中看到一个“洞”——一块缺失的瓷砖。

我可以看到请求了磁贴,并且请求完成了。我可以看到我打电话给我setNeedsDisplayInMapRect:zoomScale:,并且我正在传递原件MKMapRectMKZoomScale其中提供了canDrawMapRect:zoomScale:. 但我也可以看到,从不要求覆盖层重绘该图块(canDrawMapRect:zoomScale:drawMapRect:zoomScale:inContext:不会再次调用该图块)。

我需要了解为什么会发生这种情况以及如何纠正它。

这是我的 MKOverlayView 子类中的相关代码:

- (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];
}

编辑我猜这可能是问题所在。

4

2 回答 2

1

我遇到了一个与此处描述的问题非常相似的问题。在我的情况下,我无法重现所需的行为(在http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKOverlayView_class/Reference/Reference.html#//apple_ref/occ/instm/中描述MKOverlayView/setNeedsDisplayInMapRect:zoomScale :) 即使有最简单的代码:

- (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:从未被调用过一次。

当我开始运行setNeedsDisplayInMapRect:zoomScale:时,情况发生了变化, dispatch_async 被分派到 canDrawMapRect 运行所在的同一个队列中,例如:

- (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 - 我可以看到“这应该永远跟踪”字符串被无休止地跟踪。我原来的问题也完全消失了。

后期更新:目前,我使用 dispatch_get_main_queue() 调用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;
}
于 2012-10-26T17:35:19.097 回答
0

上面的答案对我不起作用。从我使用的 NSLog 打印输出中,我可以看到正在使用不同的线程,尽管在 canDrawMapRect 中获取了 dispatch_get_current_queue() 并将其存储以供以后使用。至少在 iPad 4.3 模拟器中就是这种情况,我没有在设备上尝试过。

我的解决方案不太令人满意且更容易出错,即在调用前等待 x 时间。

-(void)setNeedsDisplay:(WebRequest*)webRequest
{  
   [webRequest retain];
   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC), dispatch_get_main_queue(), 
   ^{
      [webRequest autorelease];
      [delegate setNeedsDisplayInMapRect:webRequest.request.requestedMapRect 
                              zoomScale:webRequest.request.requestedScale];
   });     
}
于 2012-11-09T19:28:42.143 回答