我正在使用一个由CATiledLayer支持的NSView(Mac而不是iOS),根据苹果文档https://developer.apple.com/reference/quartzcore/catiledlayer,我希望这个任务在多个线程上异步调用以提高性能,但它似乎只在主线程上被调用。
我在
我已经在NSView上启用了
在显示之前,我需要进行一些图像操作,这需要依赖于需要显示的矩形区域,并且在主线程上运行会影响性能。
有什么办法可以强制它在后台线程上调用吗?
我已经尝试将图像驱动器调度到后台线程上,然后调用以下主队列,但它会显示空白矩形:
我在
drawLayer(layer: CALayer, inContext ctx: CGContext)
中完成工作。我已经在NSView上启用了
canDrawConcurrently
,并确保窗口已设置allowsConcurrentViewDrawing
为true(默认值),但它仍然总是为每个tile 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)
})
}
})