mfessenden / SKTiled

Swift framework for working with Tiled assets in SpriteKit
Other
261 stars 33 forks source link

function addTilesetTile of SKTileset.swift crash with nil #28

Closed aornano closed 4 years ago

aornano commented 4 years ago

The function in subject crash to the line: let imageDataProvider = CGDataProvider(url: urlPath as CFURL)! This because the final path inside the bundle could be not the same as source input.

I've solved changing this part of code:

 public func addTilesetTile(_ tileID: Int, source: String) -> SKTilesetData? {
        guard !(self.tileData.contains(where: { $0.hashValue == tileID.hashValue })) else {
            log("tile data exists at id: \(tileID)", level: .error)
            return nil
        }

        // bundled images shouldn't have file paths
        //let imageName = source.componentsSeparatedByString("/").last!

        isImageCollection = true

        let inputURL = URL(fileURLWithPath: source)
        // read image from file
        let imageDataProvider = CGDataProvider(url: inputURL as CFURL)!

with:

public func addTilesetTile(_ tileID: Int, source: String) -> SKTilesetData? {
        guard !(self.tileData.contains(where: { $0.hashValue == tileID.hashValue })) else {
            log("tile data exists at id: \(tileID)", level: .error)
            return nil
        }

        // bundled images shouldn't have file paths
        //let imageName = source.componentsSeparatedByString("/").last!

        isImageCollection = true

        let inputURL = URL(fileURLWithPath: source)
        let filename = inputURL.deletingPathExtension().lastPathComponent
        let fileExtension = inputURL.pathExtension
        guard let urlPath = Bundle.main.url(forResource: filename, withExtension: fileExtension) else { return nil }

       // read image from file
        let imageDataProvider = CGDataProvider(url: urlPath as CFURL)!
mfessenden commented 4 years ago

This looks good, I'll add to the next release!