如何让CATiledLayer不阻塞主线程
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 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.