gpt4 book ai didi

swift - 从后台操作在 NSView 中绘制无效

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:14 24 4
gpt4 key购买 nike

我试图从后台操作绘制到 NSView,但没有看到任何效果。

let queue = OperationQueue()
queue.addOperation() {
doTheBackgroundStuff()
}

启动后台操作,进行大量计算。在我的 AppDelegate 中

@IBOutlet weak var image: NSImageView!  // some image to show
@IBOutlet weak var number: NSTextField! // a corresponding number
@IBOutlet weak var mainView: NSView! // the main view holding the above

作业

number.intValue = Int32(someNumber)

定期(经常)从后台操作发出。但文本永远不会改变。我已经在 IB 中为 View 和 TextField 设置了“可以同时绘制”。我也试过了

if mainView.lockFocusIfCanDraw() {
mainView.setNeedsDisplay(mainView.rectPreservedDuringLiveResize)
mainView.unlockFocus()
}

在文本字段分配之后。也无济于事。

最佳答案

按照 https://stackoverflow.com/a/19997698/3419541 中的描述调用 flushGraphics :

I read about NSGraphicsContext Restriction at Thread guide.

Here, I found the following line:

If you do any drawing from a secondary thread, you must flush your drawing calls manually. Cocoa does not automatically update views with content drawn from secondary threads, so you need to call the flushGraphics method of NSGraphicsContext when you finish your drawing. If your application draws content from the main thread only, you do not need to flush your drawing calls.

关于swift - 从后台操作在 NSView 中绘制无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40728904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com