CodexBloom - Programming Q&A Platform

Issue with Displaying High-DPI Images in NSTableView on macOS 13.6

πŸ‘€ Views: 147 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-14
macos swift NSTableView

I'm getting frustrated with I'm working on a project and hit a roadblock. I'm relatively new to this, so bear with me. I'm having trouble displaying high-DPI (Retina) images in an NSTableView on macOS 13.6. I've loaded the images from a remote server and cached them locally, but when I try to display them, they appear pixelated and don't seem to respect the high-resolution settings. I'm using the following code to configure my NSTableView cells: ```swift class CustomTableCell: NSTableCellView { @IBOutlet weak var imageView: NSImageView! @IBOutlet weak var textField: NSTextField! } class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate { var images: [NSImage] = [] override func viewDidLoad() { super.viewDidLoad() fetchImages() } func fetchImages() { // Assume this function fetches high-DPI images and stores them in the 'images' array // Example: images.append(NSImage(contentsOf: URL(string: "https://example.com/image@2x.png")!)) } func numberOfRows(in tableView: NSTableView) -> Int { return images.count } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CustomCell"), owner: self) as? CustomTableCell cell?.imageView.image = images[row] cell?.textField.stringValue = "Row \(row)" return cell } } ``` The images are being fetched correctly, but they appear low-resolution in the NSTableView. I've confirmed that the images stored in the cache are indeed high-DPI by checking their properties with `NSImageRep`. When I use `imageView.imageScaling = .scaleProportionallyUpOrDown`, it still doesn’t seem to help. I also tried setting the image view's `wantsLayer` property to true in the storyboard, but the images remain blurry. Is there a specific configuration or best practice I should follow to ensure that high-DPI images display correctly in NSTableView? Any insights would be greatly appreciated! Has anyone dealt with something similar? What would be the recommended way to handle this? This is part of a larger REST API I'm building.