gpt4 book ai didi

swift - 在 NSTouchBar 中填充 NSImageView

转载 作者:行者123 更新时间:2023-11-30 12:45:45 29 4
gpt4 key购买 nike

我目前正在创建一个小型 open source app预览新 Touch Bar 的内部结构。我有一个带有拖放 View 的窗口,可以通过其 URL 接收图像。以及一个 IB Touch Bar,其中 TouchBarView 内有一个 NSImageView。
interface builder structure (screenshot)

这在我的 MainViewController 中工作以在窗口中显示图像:

extension ViewController: DropDestinationViewDelegate {

func processImageURLs(_ urls: [URL]) {
for (_,url) in urls.enumerated() {

// pass URL to Window Controller
let windowController = WindowController()
windowController.showImageInTouchBar(url)

// create the image from the content URL
if let image = NSImage(contentsOf:url) {

imagePreviewView.image = image
}
}
}

}

如您所见,我使用委托(delegate)来监听拖放事件以获取图像 URL。现在我想使用图像/URL 在 Touch Bar 内显示相同的图像。我已经使用 Interface Builder 创建了 Touch Bar,并将图像 URL 传递给 WindowController

在我的 WindowController 中,我尝试像这样处理图像:

class WindowController: NSWindowController {

@IBOutlet var touchBarImageView: NSImageView!

override func windowDidLoad() {
super.windowDidLoad()
}

func showImageInTouchBar(_ url: URL) {
print(url)

if let touchbarImage = NSImage(contentsOf:url) {
touchBarImageView.image = touchbarImage
}
}
}

我通过 print(url) 收到了正确的 URL,但是当我尝试再次创建图像时,应用程序崩溃并显示以下消息:

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

我创建了一个包含修复的拉取请求。

您的问题是您正在创建一个新的 WindowController 并将其用作引用,而不是使用 NSWindow 创建和使用的 Controller 。

结果:

enter image description here

关于swift - 在 NSTouchBar 中填充 NSImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660017/

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