3

我有这个功能:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    TileOverlay *tileOverlay = (TileOverlay *)self.overlay;
    NSArray *tilesInRect = [tileOverlay tilesInMapRect:mapRect zoomScale:zoomScale];
    CGContextSetAlpha(context, tileAlpha);

    for (ImageTile *tile in tilesInRect)
    {
        __block UIImage * image;
        CGRect rect = [self rectForMapRect:tile.frame];

            NSString *path = [[NSString alloc] initWithFormat:@".../%@.png", tile.imagePath];
            NSLog(@"Loading tile from URL %@", path);
            image =[UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString: path]]];

            CGContextSaveGState(context);
            CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
            CGContextScaleCTM(context, 1/zoomScale, 1/zoomScale);
            CGContextTranslateCTM(context, 0, image.size.height);
            CGContextScaleCTM(context, 1, -1);
            CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), [image CGImage]);
            CGContextRestoreGState(context);
    }
}

如您所知dataWithContentsOfURL,阻塞线程直到 in 将完成。我想将图像加载块添加到该GCD部分。

我试着这样做:

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    TileOverlay *tileOverlay = (TileOverlay *)self.overlay;
    NSArray *tilesInRect = [tileOverlay tilesInMapRect:mapRect zoomScale:zoomScale];
    CGContextSetAlpha(context, tileAlpha);

    for (ImageTile *tile in tilesInRect)
    {
        __block UIImage * image;
        CGRect rect = [self rectForMapRect:tile.frame];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{
            NSString *path = [[NSString alloc] initWithFormat:@".../%@.png", tile.imagePath];
            NSLog(@"Loading tile from URL %@", path);
            image =[UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString: path]]];

            CGContextSaveGState(context);
            CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
            CGContextScaleCTM(context, 1/zoomScale, 1/zoomScale);
            CGContextTranslateCTM(context, 0, image.size.height);
            CGContextScaleCTM(context, 1, -1);
            CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), [image CGImage]);
            CGContextRestoreGState(context);
        });
    }
}

但我得到了上下文错误。请帮我解决这个问题。如何在 GCD 块中使用上下文操作?

4

1 回答 1

4

我的第一个注意事项是 MKOverlayView 已折旧。您应该考虑切换到 MKOverlayRenderer。

不存在应该在 -draw__ 方法中使用 GCD 的情况。这包括 MKOverlayView -drawMapRect:zoomScale:inContext: 以及 UIView -drawRect:。相反,您应该将 NSOperationQueue 与 -canDrawMapRect:zoomScale:zoomScale 和 setNeedsDisplayInMapRect: 结合使用。

这是它的一些 sudo 代码:

- (BOOL)canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale
{
    BOOL hasAtLeastOneTile = NO;
    TileOverlay *tileOverlay = (TileOverlay *)self.overlay;
    NSArray *tilesInRect = [tileOverlay tilesInMapRect:mapRect zoomScale:zoomScale];

    for (ImageTile *tile in tilesInRect) {
        if ([tile isAvailable]) {
            hasAtLeastOneTile = hasAtLeastOneTile || YES;
        } else {
            // Add operation to NSOperationQueue to fetch tile
            __weak MKOverlayView *weakOverlay = self; // Weak ref to prevent retain cycles
            NSOperation *op = [NSBlockOperation blockOperationWithBlock: ^{
                //TODO: Load Tile
                [weakOverlay setNeedsDisplayInMapRect:mapRect];
            }];
            [self.operationQueue addOperation:op];
        }
    }
    return hasAtLeastOneTile;
}

然后在您的 -drawMapRect:zoomScale:inContext: 中绘制可用的图块并跳过不可用的图块。

于 2013-10-18T13:21:17.787 回答