How do you get the original filename of a sprite/texture in Swift?

烂漫一生 提交于 2019-12-23 03:10:56

问题


I create a sprite and assign an image file to it.

var logoImage = SKSpriteNode(imageNamed: "image1.png")

Then in some circumstances, I change the image.

logoImage.texture = SKTexture(imageNamed: "image2.png")

In another part of the app I want to check which image is currently being displayed. But I don't know how to get the filename.

Using:

print(logoImage.texture?.description)

Returns:

"<SKTexture> 'image2.png' (500 x 500)"

Which obviously contains the filename, but how do I get the filename on it's own?


回答1:


I suggest your to avoid exposing the internal logic of your objects.

And definitely you should NOT build your game logic on the texture currently used. The presentation should be a mere representation of the actual data.

So you should create your own class that wraps logic and data, like this.

class Logo: SKSpriteNode {

    enum Type: String {
        case A = "Image1", B = "Image2"
        var imageName: String {
            return self.rawValue
        }
    }

    private var type: Type {
        didSet {
            self.texture = SKTexture(imageNamed: type.imageName)
        }
    }

    init(type: Type) {
        self.type = type
        let texture = SKTexture(imageNamed: type.imageName)
        super.init(texture: texture, color: .clearColor(), size: texture.size())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Usage

Now you can easily create a Logo

let logo = Logo(type: .A)

You can change the texture for that sprite

logo.type = .B

And you can check what texture is currently using

switch logo.type {
case .A: print("It's using Image1")
case .B: print("it's using Image2")
}

Last thing. I replaced Image1.png and Image2.png with Image1 and Image2. If you are using Asset Catalog (and you should) then you don't need to specify the file extension.




回答2:


You could use:

if logoImage.texture?.descriptio.containsString("image1.png") {
   // your code for image1 here
} else {
   // your code for image2 here
}

You'll have use:

import Foundation

in your code for containsString




回答3:


I'm pretty sure that there is a more elegant method but it works:

if let rangeOfIndex = texture.description.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "'"), options: .BackwardsSearch) {
   let filename = texture.description.substringToIndex(rangeOfIndex.endIndex)
   if let r = filename.rangeOfCharacterFromSet(NSCharacterSet(charactersInString: "'"), options: .LiteralSearch) {
      print(filename.substringFromIndex(r.startIndex).stringByReplacingOccurrencesOfString("'", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil))
   }  
}



回答4:


You could use this :

func getTextureName(textureTmp: String) -> String {
    var texture:String = ""
    var startInput = false
    for char in textureTmp {
        if startInput {
            if char != "'" {
                texture += String(char)
            } else {
                return texture
            }
        }
        if char == "'" {
            startInput = true
        }
    }
    return texture
}

Its not beautiful but it works



来源:https://stackoverflow.com/questions/37296496/how-do-you-get-the-original-filename-of-a-sprite-texture-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!