[英]Unable to set Image of NSImageView using file path
我已經迷上了 NSTableView 的選擇更改事件。我需要在 imageview 中顯示所選圖像
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])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.