views:

234

answers:

1

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.

A: 

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
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