SKTexture和UIImage的scale属性

Pie*_*ter 10 objective-c uiimage ios retina-display sprite-kit

是否有任何理由为什么在构建时SKTexture似乎忽略了.scale图像textureWithImage:

我有一个可用的图像资源,"retina_image@2x.png"

当我在创建UIImage时尝试创建纹理时:

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));
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

image size {50, 50}, scale 2.000000
texture from image, size {100, 100}
Run Code Online (Sandbox Code Playgroud)

虽然我希望得到

image size {50, 50}, scale 2.000000
texture from image, size {50, 50}
Run Code Online (Sandbox Code Playgroud)

因为图像的大小(以磅
为单位)是50x50 这也是您使用资源直接构造纹理时获得的内容:

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

给出以下输出(如预期的那样):

texture named, size {50, 50}
Run Code Online (Sandbox Code Playgroud)

这表明SKTexture在从图像构造时确定其自身大小时忽略scale属性,同时在从imagename构造时适当地遵守比例.
这是预期的行为吗?

(显然在我的真实代码中,我创建了我想要以编程方式在纹理中使用的UIImage,而不是通过imageNamed)

编辑:这是SpriteKit中的(已确认)错误,已在iOS8中修复

小智 9

我也发现了这一点,并认为这是一个错误.不确定Apple将如何纠正这一点,因为它可能会破坏现有代码.

我的解决方法是读取UIImage的比例,然后将SKSpriteNode的比例设置为1.0 /比例.您需要对SKSpriteNode的x和y标度执行此操作.

  • 这可能是一个问题,因为UIImage世界和SpriteKit世界中的"规模"是两个不同的东西.UIImage上的较高比例使得图像在屏幕上"更小",与SK相反......您将永远无法在不转换的情况下交替使用两个API的同等拼写的"比例". (2认同)