I'm testing a game on the OS X version of Sprite Kit, and find that some textures report their size incorrectly. I'm wondering if anyone else is seeing this problem and whether there is an explanation for it?

Just to rule out any other issues before I report this as a bug. If this is a known bug, please say so.

I'm loading this texture, it is 256x256 pixels in size:

enter image description here

When I log the SKTexture created from this image, then the SKTexture description gives me the correct size but the size property does not, it says the image is 204.8 x 204.8.

tex = <SKTexture> 'Tileset_GeometryB.png' (256 x 256)
tex.size = {204.80000000000001, 204.80000000000001}  // Huh? WTF?!?
tex.textureRect = {{0, 0}, {1, 1}}

I load the exact same texture using the same code on iOS Simulator and device - size property is always correct: 256x256.

I also tried loading this texture before anything else just to avoid any possible side effects. Still the texture size is wrong.

It is worth noting that this doesn't happen with all textures, but does happen on all 256x256 textures I load. A 128x256 texture reports its size correctly.

I also did a clean build, and I'm testing on Mavericks (13A603) with Xcode 5.0.1 (5A2053).

有帮助吗?

解决方案

Apparently this issue is caused by incompatible image programs, in my case Inkscape was always the culprit.

The PNG files created by Inkscape appeared to work correctly but cause Sprite Kit to report the size incorrectly. In every instance this happened to me, the fix was simply to open the PNG file in Seashore, then "Save As" under the same filename to force the program to re-save the PNG file.

I assume opening and saving with other image programs might work as well, perhaps even Preview might help to fix this or a command line tool like PNGCrush.

There's also a certain consistency because Inkscape texture size is reported as 20% less, ie 204.8 for a 256 texture, or 0.8 for a 1-pixel texture. The problem seems to be with Inkscape's ppi (pixels per inch) setting, where it does not allow values below 90 ppi. But Photoshop's default is 72 ppi which is exactly 20% less. And I take it that Sprite Kit assumes the PNG to be 72 ppi, gets a 90 ppi image, and thus incorrectly (or correctly?) calculates the PNG's size as 20% less than what it actually is.

其他提示

Do you create texture with regular methods or actually show it on screen? I had similar issue with animations today, and I think this has to do with lazy lading of textures. They don't actually load until the time you have to use them. Try using this and checking size:

    [tempTexture preloadWithCompletionHandler:^{}];

This also might explain why the rect is 0,0,1,1

In the Finder image preview, you can see the Dimensions and the Resolution of the image. The resolution comes from the EXIF information. The simple solution is to remove that EXIF information.

You can easily achieve that with ImageMagick:

convert image.png -strip image-with-no-exif.png

or you can modify the image in-place:

convert -strip image.png

If you look now in Finder, the Resolution field should be gone.

You can also inspect the detailed EXIF information with:

identify -verbose image.png

Once stripped of the EXIF information, the size reported by SpriteKit is the size in pixels (tested on Xcode 12.2).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top