我目前正在创建一个小型 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
最佳答案
关于swift - 在 NSTouchBar 中填充 NSImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660017/