Retina version of an image always used on non-retina display

耗尽温柔 提交于 2020-01-14 13:47:11

问题


In Cocoa application I've got 16x16 and 32x32 @2x version of an image:

. When the image is displayed in NSImageView Mac OS X always picks higher-resolution version (i.e. downscales the @2x image on non-retina displays instead of using the 1:1 version).


(IB on the left = good, running app on the right = downscaled mess)

Of course I've got both images added to the project (as image.png and image@2x.png).

If I delete the @2x image from app bundle then OS X will display the lower-resolution image.

The bug happens regardless whether Xcode (4.6.2) combines them into a .tiff or not (and I've checked that the combined .tiff contains both images).

Oddly this happens only with this particular image. Other 1x/2x images in the same project are displayed correctly matching screen DPI.

How is that possible?! Do images have to meet some special criteria other than size and filename pattern?


回答1:


It turns out to be the NSImage’s prefersColorMatch property [1]:

The default value is YES. Both color matching and resolution matching may influence the choice of an image representation.

It is possible to set to NO in User Defined Runtime Attributes in Interface Builder [2].

[1] https://developer.apple.com/documentation/appkit/nsimage/1520010-preferscolormatch

[2] Are specific PNG compression types incompatible with macOS Cocoa apps?




回答2:


Mystery solved: OS X doesn't like mixed types of PNGs.

$ file *.png
image.png: PNG image data, 16 x 16, 8-bit gray+alpha, non-interlaced
image@2x.png: PNG image data, 32 x 32, 8-bit colormap, non-interlaced

if both files are forced to use same color mode (i.e. both gray or both paletted) then OS X selects images correctly.



来源:https://stackoverflow.com/questions/16660082/retina-version-of-an-image-always-used-on-non-retina-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!