0

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 control over.

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

rickharrison
  • 4,867
  • 4
  • 35
  • 40

1 Answers1

0

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.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • So after the data is downloaded and drawn to the screen should I release the UIImage? If I pan away and come back will it still be drawn there? – rickharrison Jun 04 '10 at 13:35
  • The data is cached, but you can't control the caching, so your code should expect that the drawLayer:inContext could be called repeatedly – JakubKnejzlik May 21 '14 at 16:21