Вопрос

Is there any reason why an SKTexture appears to ignore the .scale of an image when constructed via textureWithImage:?

I have one image resource available, "retina_image@2x.png"

When I try to create a texture while creating a UIImage first:

UIImage* image = [UIImage imageNamed:@"retina_image"];
SKTexture* texture_image = [SKTexture textureWithImage:image];
NSLog(@"image size %@, scale %f", NSStringFromCGSize(image.size), image.scale);
NSLog(@"texture from image, size %@", NSStringFromCGSize(texture_image.size));

I get the following result:

image size {50, 50}, scale 2.000000
texture from image, size {100, 100}

While I would expect to get

image size {50, 50}, scale 2.000000
texture from image, size {50, 50}

As the size of the image (in points) is 50x50
This is also what you get when you construct the texture with the resource directly:

SKTexture* texture_named = [SKTexture textureWithImageNamed:@"retina_image"];
NSLog(@"texture named, size %@", NSStringFromCGSize(texture_named.size));

gives the following output (as expected):

texture named, size {50, 50}

This suggests SKTexture ignores the scale property when determining its own size when constructed form an image, while properly respecting the scale when constructed from the imagename.
Is this expected behaviour?

(obviously in my real code I create the UIImage I want to use in a texture programmatically and not via imageNamed)

Edit: This is a (confirmed) bug in SpriteKit, fixed in iOS8

Это было полезно?

Решение

I've found this too and believe it's a bug. Not sure how Apple are going to rectify this as it could break existing code.

My workaround is to read the scale of the UIImage and then set the scale of the SKSpriteNode to 1.0 / scale. You need to do this for both the x and y scales of the SKSpriteNode.

Другие советы

From apples doc's of UIImage

On a device running iOS 4 or later, the behavior is identical if the device’s screen has a scale of 1.0. If the screen has a scale of 2.0, this method first searches for an image file with the same filename with an @2x suffix appended to it. For example, if the file’s name is button, it first searches for button@2x. If it finds a 2x, it loads that image and sets the scale property of the returned UIImage object to 2.0. Otherwise, it loads the unmodified filename and sets the scale property to 1.0. See iOS App Programming Guide for more information on supporting images with different scale factors.

But according to textureWithImageNamed:.

The new texture object is initialized with the name of the image file and then control returns immediately to your game. Sprite Kit loads and prepares the texture data when it is needed by your game.

When loading the texture data, Sprite Kit searches the app bundle for an image file with the specified filename. If a matching image file cannot be found, Sprite Kit searches for the texture in any texture atlases stored in the app bundle. If the specified image does not exist anywhere in the bundle, Sprite Kit creates a placeholder texture image.

From the above textureWithImageNamed: and SKTexture doesn't care about scaling with device.(retina image).

I have the same behavior with textures from atlas created from a dictionary. On iOS 8.3

How to force SKTextureAtlas created from a dictionary to not modify textures size?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top