如何将图像文件夹添加到我的 FileWrapper

How do I add an Images folder to my FileWrapper

我需要一个包含文件和文件夹的 FileWrapper。 文件为单个文件,文件夹用于写入图片。

该文件夹还可以包含一些子文件夹。我有一个工作代码,但问题是当文档被保存时,文件夹被重写,这会删除我的图像和子文件夹。

我很确定它与 func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper 有关,但我需要对 FileWrappers 有更多经验的人的帮助。

这是我的代码:

struct MyProject: FileDocument {
    var myFile: MyFile
    
    static var readableContentTypes: [UTType] { [.myf] }

    init(myFile: MyFile = MyFile() {
        self.myFile = myFile
    }

    init(configuration: ReadConfiguration) throws {
        let decoder = JSONDecoder()
        guard let data = configuration.file.fileWrappers?["MYFProject"]?.regularFileContents else {
            throw CocoaError(.fileReadCorruptFile)
        }
        do {
            self.myFile = try decoder.decode(MyFile.self, from: data)
        } catch {
            throw error
        }
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let encoder = JSONEncoder()
        do {
            let data = try encoder.encode(myFile)
            
            let mainDirectory = FileWrapper(directoryWithFileWrappers: [:])
            let myfWrapper = FileWrapper(regularFileWithContents: data)
            let imagesWrapper = FileWrapper(directoryWithFileWrappers: [:])
            let imageSubFolder = FileWrapper(directoryWithFileWrappers: [:])
        
            for numberString in myFile.numbers {
                imageSubFolder.preferredFilename = numberString
                imagesWrapper.addFileWrapper(imageSubFolder)
            }
            
            myfWrapper.preferredFilename = "MYFProject"
            mainDirectory.addFileWrapper(myfWrapper)
            
            imagesWrapper.preferredFilename = "MYFImages"
            mainDirectory.addFileWrapper(imagesWrapper)
            
            return mainDirectory
        } catch {
            throw error
        }
    }
}

我用这个路径写图片。

func getSubFolderImageFolder(documentPath: URL, subFolder: String) -> URL {
    let sfProjectPath = documentPath.appendingPathComponent("MYFImages").appendingPathComponent(subFolder)
    
    if !FileManager.default.fileExists(atPath: sfProjectPath.path) {
        do {
            try FileManager.default.createDirectory(atPath: sfProjectPath.path, withIntermediateDirectories: false, attributes: nil)
            return sfProjectPath
        } catch {
            fatalError(error.localizedDescription)
        }
    }
    else {
        return sfProjectPath
    }
}

提前致谢!

您的 getSubFolderImageFolder 函数不能很好地与文件包装器一起使用。您必须使用 FileWrapper 方法在文件包装器中创建文件夹和文件。

要将子文件夹添加到图像文件夹,请按照为图像创建 imagesWrapper 文件夹的相同方式创建目录文件包装器。将子文件夹添加为图像文件夹的子文件夹。

let imageSubFolder = FileWrapper(directoryWithFileWrappers: [:])
imagesWrapper.addFileWrapper(imageSubFolder)

您必须为每个子文件夹创建一个目录文件包装器。我注意到在您更新的代码中,您只有一个子文件夹文件包装器。只有一个子文件夹文件包装器,您无法将图像文件存储在正确的子文件夹中。

要添加图像,首先将每个图像转换为 Data 对象。为每个图像创建一个常规文件包装器,将图像数据作为参数传递给 regularFileWithContents。调用 addFileWrapper 将图像文件添加到适当的文件夹。

let imageFile = FileWrapper(regularFileWithContents: imageData)
imageFile.preferredFilename = "ImageFilename" // Replace with your filename.
imagesWrapper.addFileWrapper(imageFile)

在您的情况下,图像子文件夹将调用 addFileWrapper 来添加图像。图像文件的目的地调用 addFileWrapper.

您可以在以下文章中找到有关文件包装器的更多详细信息:

Using File Wrappers in a SwiftUI App