我正在使用CATiledLayer支持的NSView(Mac不是iOS),并且根据Apple文档https://developer.apple.com/reference/quartzcore/catiledlayer我期望在多线程上可以异步调用它以提高性能,但它似乎只能在主线程中调用。CATiledLayer drawLayer:inContext始终在主线程上调用 - MacOS El Capitan
我做我已经在一个NSView启用canDrawConcurrently
在drawLayer(layer: CALayer, inContext ctx: CGContext)
工作以及确保窗口allowsConcurrentViewDrawing
设置为true(默认),但它仍然总是用于每个图块的主线程调用上RECT。
根据需要显示的矩形区域,我需要在显示之前进行一些图像处理,并且在主线程上运行会影响性能。
任何想法如何强迫它调用后台线程?
我试图编组图像的操作到后台线程,然后回顾主队列以下,但它显示一个空白矩形:
backgroundQueue.addOperationWithBlock({ [ weak self ] in
guard let strongSelf = self else { return }
// ** This is the part I nee to do in background that can take some time
guard let image = strongSelf.imageForTileRect(layerBounds, imageSize: imageSize) else { return }
// ** End background bit
var rect = layerBounds
let rectImageRef = image.CGImageForProposedRect(&rect, context: nil, hints: nil)
if (rectImageRef != nil) {
NSOperationQueue.mainQueue().addOperationWithBlock({
CGContextDrawImage(ctx, rect, rectImageRef)
})
}
})
你有没有想出解决办法?我看到了同样的事情。我想使用后台线程绘制到我的NSView的CATiledLayer。 –
这里有更多的上下文。如果我将另一个CATiledLayer作为叠加层添加到作为我的后备叠加层的叠加层中,它将在后台呈现。只是主视图在主线程上呈现的主体。我最终可能会将主要部分变成空白,并将渲染放到覆盖层CATiledLayer中。 –