如何让CATiledLayer不阻塞主线程

发布于 2024-09-03 16:39:46 字数 718 浏览 2 评论 0原文

我正在将 CATiledLayer 实现到 UIScrollView 中。在CATiledLayer中,我有一个绘制图层的函数,如下所示:

- (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];
}

问题是,当每个图块下载时,它会阻止其他图块的下载。我非常希望这些图块能够并行下载。特别是它会阻止我无法控制的另一个 UI 元素的下载。

基本上,我只需要知道如何在 CATiledLayer 绘图消息中异步下载数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-09-10 16:39:46

您可以使用 NSURLConnection 之类的工具异步下载数据,就像在任何其他情况下一样。下载完成后,告诉图层重新绘制,然后调用 -drawLayer:inContext: 此时您只需抓取已下载的图像。换句话说,不要在 -drawLayer 中下载数据,也不要使用 -dataWithContentsOfURL,它是同步的,默认情况下会阻塞。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文