【发布时间】:2015-07-22 08:03:56
【问题描述】:
在我的项目中,textures 是由 PaintCode (paint-code) 提供的方法按程序生成的。
然后我从通过这些方法生成的带有UIImage 的字典中创建一个SKTextureAtlas:myAtlas = SKTextureAtlas(dictionary: myTextures)
最后,使用textureNamed从atlas中检索纹理:
var sprite1 = SKSpriteNode(texture:myAtlas.textureNamed("texture1"))
但显示的 节点 在 iPhone4S 模拟器上是双倍大小。并在 iPhone 6 Plus 模拟器上放大三倍。
似乎在初始化时,atlas 以设备分辨率计算图像。 但是生成的图像已经具有正确的大小,不需要更改。请参阅下面的绘图方法。
这里是生成图片的说明:<UIImage: 0x7f86cae56cd0>, {52, 52}
以及图集中对应纹理的描述:<SKTexture> 'image1' (156 x 156)
这适用于 iPhone 6 Plus,使用 @3x 图像,这就是大小为 x3 的原因。
对于 iPhone 4S,使用 @2x 图像,正如预期的那样:<UIImage: 0x7d55dde0>, {52, 52}<SKTexture> 'image1' (156 x 156)
最后,生成的UIImage 的scale属性设置为正确的设备分辨率:@2x (iPhone 4S) 为 2.0,@3x (iPhone 6 Plus) 为 3.0。
问题
那么我该怎么做才能避免图集调整图片大小?
画法
PaintCode生成绘图方法如下:
public class func imageOfCell(#frame: CGRect) -> UIImage {
UIGraphicsBeginImageContextWithOptions(frame.size, false, 0)
StyleKit.drawCell(frame: frame)
let imageOfCell = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return imageOfCell
}
更新 1
比较两种生成SKTextureAtlas的方法
// Some test image
let testImage:UIImage...
// Atlas creation
var myTextures = [String:UIImage]()
myTextures["texture1"] = testImage
myAtlas = SKTextureAtlas(dictionary: myTextures)
// Create two textures from the same image
let texture1 = myAtlas.textureNamed("texture1")
let texture2 = SKTexture(image:testImage)
// Wrong display : node is oversized
var sprite1 = SKSpriteNode(texture:texture1)
// Correct display
var sprite2 = SKSpriteNode(texture:texture2)
问题似乎出在字典中的SKTextureAtlas 上,因为SKSpriteNode 初始化不使用UIImage 中的scale 属性来正确调整节点的大小。
以下是控制台上的说明: - texture1: '' (84 x 84) - texture2: 'texture1' (84 x 84)
texture2 想念一些data!这可以解释缺乏 scale 信息来正确调整节点大小:
节点的大小 = 纹理的大小除以纹理的比例。
更新 2
UIImage 的 scale 属性不为 1 时会出现问题。
所以可以使用以下方法生成图片:
func imageOfCell(frame: CGRect, color:SKColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(frame.size, false, 0)
var bezierPath = UIBezierPath(rect: frame)
color.setFill()
bezierPath.fill()
let imageOfCell = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return imageOfCell
}
【问题讨论】:
-
iOS 9 / Xcode 7 中仍然存在问题...
标签: paint-code swift uiimage sprite-kit sktextureatlas paintcode