swift - 在 NSTouchBar 中填充 NSImageView

标签 swift macos macos-sierra nstouchbar

我目前正在创建一个小型 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/

相关文章:

ios - 无效的 App Store 图标

vmware - 如何修复VMDK损坏文件恢复?

swift - 为什么 sprite 没有碰撞却相互接触?

ios - 使用 Swift 解码日期时出错

windows - Mac 和 Windows 之间的 Matlab GUI 兼容性(显示)

python - 在 Mac OS X 10.6 上重新安装 Python 2.6

macos - 在 iCloud Drive 中进行 latex 化

Swift 2 - 具有多个部分的 UITableView

swift - placesClient - 正确使用 sharedClient 方法?

java - 在非 MACOS JRE 上捕获 java.lang.NoClassDefFoundError