我有这个功能:

- (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直到完成阻塞线程。我想将图像加载块添加到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块中使用上下文操作?

最佳答案

我的第一个注意事项是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中:绘制可用的图块,并跳过不可用的图块。

10-08 05:28