Folder of images: create and fill SKShapeNodes, 1 of each

后端 未结 2 578
栀梦
栀梦 2020-12-20 05:25

Is it possible to create a folder and have SpriteKit go through that folder, find each image (regardless of name), and create a SKShapeNode for each in that folder (regardle

相关标签:
2条回答
  • 2020-12-20 05:58

    Too many requests , so I try to help you with some code:

    Create a folder:

    func createDir(folderName:String) {
            let fileManager = FileManager.default
            if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "APP_GROUP_IDENTIFIER") {
                let newDirectory = directory.appendingPathComponent(folderName)
                try! fileManager.createDirectory(at: newDirectory, withIntermediateDirectories: false, attributes: nil)
            }
    }
    

    Now you have your folder so you can decide to save a file or to copy one file from documents to this new directory. I make an example for the last one.

    Copy a file:

    let fileManager = FileManager.default
    do {
       try fileManager.copyItem(atPath: "hero.png", toPath: "subfolder/hero.swift")
    }
    catch let error as NSError {
       print("Something went wrong: \(error)")
    }
    

    Now you want to know how to extract all files from a directory.

    Extract all files from a directory:

    func extractAllFile(atPath path: String, withExtension fileExtension:String) -> [String] {
            let pathURL = NSURL(fileURLWithPath: path, isDirectory: true)
            var allFiles: [String] = []
            let fileManager = FileManager.default
            if let enumerator = fileManager.enumerator(atPath: path) {
                for file in enumerator {
                    if #available(iOS 9.0, *) {
                        if let path = NSURL(fileURLWithPath: file as! String, relativeTo: pathURL as URL).path
                            , path.hasSuffix(".\(fileExtension)"){
                            allFiles.append(path)
                        }
                    } else {
                        // Fallback on earlier versions
                    }
                }
            }
            return allFiles
        }
    

    Usage:

    let folderPath = Bundle.main.path(forResource: "Images", ofType: nil)
    let allPngFiles = extractAllFile(atPath: folderPath!, withExtension: "png") // returns file path of all the png files inside the folder
    

    After that, you can create an array of SKShapeNode based from allPngFiles using fillTexture with a SKTexture created from each image and give to each node the name of the file used (so you know at the end also how many nodes you have created from array.count).

    0 讨论(0)
  • 2020-12-20 06:14

    Slightly different answer here. I'm not sure why one needs to create a directory and copy files over. In your screenshot you have a demoArt.atlas with 7 files (one.png, two.png, etc). You indicate you want to generate an SKShapeNode. It still isn't clear to me why you want SKShapeNode, but ignoring that, you just need to know the folder name. You'll notice how the folder is blue, which means it is a reference. This means that in the image, the folder is retained.

    This would get you all the files in that directory:

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let texPath = NSURL(fileURLWithPath: documentsPath).appendingPathComponent("demoArt.atlas")
    
    let filemanager:FileManager = FileManager()
    // Error check/ensure path exists
    let files = filemanager.enumerator(atPath:texPath!.path)
    
    while let file = files?.nextObject() {
        print(file)
        // Create your SKShapeNode here, you can load file from this
        // This is all relative path, so "file" would be one.png, two.png, etc. If you need the full path, you
        // need to prepend texPath
    }
    

    Note the key diff here is not copying files over. Maybe I just don't understand the problem and why it would require the copy.

    If you wanted to put that in an array you can. But here you'll have the file name of the texture which you would of course need to load.

    Realistically, you want to load those textures asynchronously first, then construct your SKShapeNode.

    0 讨论(0)
提交回复
热议问题