无法使用文件路径设置 NSImageView 的图像

Unable to set Image of NSImageView using file path

我已经连接到 NSTableView.I 的 Selection changed 事件需要在图像视图中显示所选图像

func tableViewSelectionDidChange(_ notification: Notification)
       {
        let table = notification.object as! NSTableView

        print(fileArray[table.selectedRow].path);
        img_view.image=NSImage(named: NSImage.Name(rawValue: fileArray[table.selectedRow].path))
       }

控制台打印

/Users/myname/Downloads/435_v9_bc.jpg

但是imageview不显示图片

更新 1:

 print(fileArray[table.selectedRow].path);
 img_view.image=NSImage(byReferencing: URL(string: fileArray[table.selectedRow].path)!)

线程 1:致命错误:在展开可选值时意外发现 nil

控制台仍然打印

/Users/myname/Downloads/123_1 (1).jpeg

URL(string:是错误的API,对于文件系统路径你必须使用URL(fileURLWithPath.

img_view.image = NSImage(byReferencing: URL(fileURLWithPath: fileArray[table.selectedRow].path))

但是 fileArray[table.selectedRow] 似乎已经是 URL 所以还有更简单的方法

img_view.image = NSImage(contentsOf: fileArray[table.selectedRow])