如何从SKTexture获取原始图像比例?

时间:2015-04-27 03:10:40

标签: ios swift sprite-kit sktexture

我在Swift中使用sprite-kit并使用SKTexture(rect:inTexture:)函数设置纹理。我没有使用内置纹理图集,而是使用SKTexture(imageNamed:)创建父纹理(纹理图集)。这是因为我的数据文件中已有地图集和子纹理位置(以像素为单位),而不是单独的图像。

当图像为@ 2x(大小为像素尺寸的一半)时,.size()对象的SKTexture是正确的。我通过除以大小来计算子矩形坐标(在单位坐标中,0-1),但我需要知道原始图像比例,以便我可以用它来划分像素尺寸。

这是我的代码很好用:

// Create the texture as a sub-rect of the parent texture
if  let parentTexture = textures[parentTextureId] {
    let size = parentTexture.size()
    var subRect = CGRectFromString(subRectString)
    subRect = CGRectMake(
        (subRect.origin.x / 2) / size.width,
        (size.height - ((subRect.origin.y / 2) + (subRect.size.height / 2))) / size.height,
        (subRect.size.width / 2) / size.width,
        (subRect.size.height / 2) / size.height)
    let texture = SKTexture(rect: subRect, inTexture: parentTexture)
    textures[textureId] = texture
}

问题在于我必须对所有/ 2调整进行硬编码。如何获得纹理的原始图像比例(或实际像素尺寸),以便我可以正确计算子矩形而无需硬编码?

0 个答案:

没有答案