Search code examples
iosobjective-cuiimageretina-displaysprite-kit

SKTexture and the scale property of a UIImage


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, "[email protected]"

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


Solution

  • 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.