SKTexture和UIImage的scale属性 [英] SKTexture and the scale property of a UIImage

查看:117
本文介绍了SKTexture和UIImage的scale属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过textureWithImage:构造时,SKTexture是否似乎忽略图像的.scale吗?

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

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

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

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

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));

我得到以下结果:

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

虽然我希望得到

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

图像的大小(以磅为单位)为50x50
直接使用资源构造纹理时,也会得到以下结果:

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}

这建议SKTexture在从图像构造时确定其自身大小时会忽略scale属性,而在从imagename构造时应适当考虑比例.
这是预期的行为吗?

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?

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

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

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

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.

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

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.

这篇关于SKTexture和UIImage的scale属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆