开发者

How to have CATiledLayer not block the main thread

I am implementing a CATiledLayer into a UIScrollView. In the CATiledLayer, I have a function to draw the layers like so:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CGContextTranslateCTM(ctx, 0.0f, 0.0f);
    CGContextScaleCTM(ctx, 1.0f, -1.0f);

    CGRect box = CGContextGetClipBoundingBox(ctx);

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"urlhere"]];
    UIImage *image = [[UIImage alloc] initWithData:data];

    CGContextDrawImage(ctx, box, [image CGImage]);
    [image release];
    [data release];
}

The problem is that when each tile is downloading it blocks the downloads of other tiles. I would very much prefer if these tiles were downloaded in parallel. In particular it blocks the downloads of another UI element that I am not in开发者_如何学Python control over.

Basically, I just need to know how to download data asynchronously in a CATiledLayer drawing message.


You download the data asynchronously as you would in any other situation using something like NSURLConnection. When the download has completed, tell the layer to re-draw which will then call -drawLayer:inContext: at which point you just grab the image that was downloaded. In other words, don't download your data in -drawLayer and don't use -dataWithContentsOfURL which is synchronous and blocks by default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜