gpt4 book ai didi

swift - 在 640x852 像素的 NSImageView 中显示 500x500 像素的图像,没有任何种类或模糊(OS X 上的 Swift)

转载 作者:可可西里 更新时间:2023-11-01 01:00:12 25 4
gpt4 key购买 nike

我花了几个小时在谷歌上搜索这个问题的答案并尝试了一些代码,但我就是找不到。我也知道这是一个被问过很多次的问题,但是我不知道现在还能做什么。

我可以从 Met Offices 的 DataPoint API 访问覆盖英国的 500x500 像素降雨雷达图像。它们必须显示在 640x852 像素区域(一个 NSImageView,我目前将其缩放属性设置为与轴无关),因为这是为图像覆盖的边界生成的 map 的正确大小。我想使用最近邻算法以别名格式以 640x852 的放大尺寸显示它们。这可以在 Photoshop 中通过转到“图像”>“图像大小...”并将重新采样设置为最近的邻居(硬边)来实现。源图像应保持 500x500 像素,我只想在更大的 View 中显示它们。

我已经尝试将 NSImageView.layer 的 magnificationFilter 设置为所有三个不同的 kCAFilter... 选项,但这没有任何区别。我还尝试将 NSImageView.layer 的 shouldRasterize 属性设置为 true,这也没有效果。图像总是最终被平滑或消除锯齿,这是我不想要的。

最近刚从 C# 转过来,可能有一些我遗漏的东西,因为我已经很长时间没有使用 Swift 编程了。在 C# 中(使用 WPF),我能够通过将图像元素的 BitmapScalingOptions 设置为 NearestNeighbour 来获得我想要的内容。

总而言之,我想使用 Swift 在像素化形式的 640x852 像素 NSImageView 中显示 500x500 像素的图像,而不进行任何类型的平滑处理(无论显示器是否为视网膜)。感谢您能给我的任何帮助。

图片来源如下: this is the image source

下面是实际结果(截图来自 5K iMac): this is the actual result这是通过使用我的 NSTableView 的 tableViewSelectionDidChange 事件简单地设置 NSImageSource 上的图像属性来创建的,该事件用于选择显示图像的时间,使用:

let selected = times[timesTable.selectedRow]

let formatter = NSDateFormatter()
formatter.dateFormat = "d/M/yyyy 'at' HH:mm"
let date = formatter.dateFromString(selected)
formatter.dateFormat = "yyyyMMdd'T'HHmmss"

imageData.image = NSImage(contentsOfFile: basePathStr +
"RainObs_" + formatter.stringFromDate(date!) + ".png")

下面是我想要的样子(忽略背景和裁剪部分)。如果你自己保存图像,你会看到它是像素化的和别名的: this is what I want it to look like

下面是显示源的 map (源只是位于位于另一个包含 map 的 NSImageView 之上的 NSImageView 中): enter image description here

最佳答案

尝试使用 NSView 的自定义子类代替 NSImageView。它需要一个 image 属性和一个设置 needsDisplaydidSet 观察器。在 drawRect() 方法中,要么:

  • 使用 NSImagedrawInRect(_:fromRect:operation:fraction:respectFlipped:hints:) 方法和 hints 字典[NSImageHintInterpolation:NSImageInterpolation.None],或者
  • 保存NSGraphicsContext.currentContext.imageInterpolation的当前值,将其更改为.None,使用任意绘制NSImage >draw...(...) 方法,然后恢复上下文的原始 imageInterpolation

关于swift - 在 640x852 像素的 NSImageView 中显示 500x500 像素的图像,没有任何种类或模糊(OS X 上的 Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com