Jon Winstanley
Jon Winstanley

Reputation: 23321

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

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?

Upvotes: 1

Views: 1070

Answers (4)

Daniel Hall
Daniel Hall

Reputation: 854

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

Upvotes: 0

Luca Angeletti
Luca Angeletti

Reputation: 59536

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.

Upvotes: 4

Alessandro Ornano
Alessandro Ornano

Reputation: 35412

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

Upvotes: 0

Steve Ives
Steve Ives

Reputation: 8134

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

Upvotes: 0

Related Questions