文件“;xxx和x201D;不能’;不能保存在文件夹“;xxx”;-未在swift macos中保存NSOpenPanel选定url的NSImage

文件“;xxx和x201D;不能’;不能保存在文件夹“;xxx”;-未在swift macos中保存NSOpenPanel选定url的NSImage,swift,macos,swift5,nsimage,nsopenpanel,Swift,Macos,Swift5,Nsimage,Nsopenpanel,我是MACOS开发和制作应用程序的新手。在我的应用程序中,我打开NSOpenPanel并选择一个文件夹或任何目录(如下载、文档),然后NSOpenPanel begin方法将为我提供所选的URL。我想在选定的URL上保存多个图像,但遇到此错误时无法实现 无法将文件“xxx”保存在文件夹“xxx”中 @IBAction func menuExportTapped(_ sender: NSMenuItem) { let openPanel = NSOpenPanel()

我是MACOS开发和制作应用程序的新手。在我的应用程序中,我打开NSOpenPanel并选择一个文件夹或任何目录(如下载、文档),然后NSOpenPanel begin方法将为我提供所选的URL。我想在选定的URL上保存多个图像,但遇到此错误时无法实现

无法将文件“xxx”保存在文件夹“xxx”中

@IBAction func menuExportTapped(_ sender: NSMenuItem) {
        let openPanel = NSOpenPanel()
        openPanel.canChooseFiles = false
        openPanel.allowsMultipleSelection = false
        openPanel.canChooseDirectories = true
        openPanel.canCreateDirectories = true
        openPanel.title = NSLocalizedString("change_the_folder", comment: "")
        openPanel.runModal()
        openPanel.begin { [weak self] (result) -> Void in
            if result == .OK {
                self?.myImage?.writePNG(toURL: openPanel.url!)
            } else {
                openPanel.close()
            }
        }
}
我测试过写代码,工作正常

extension NSImage {
public func writePNG(toURL url: URL) {
        
        guard let data = tiffRepresentation,
              let rep = NSBitmapImageRep(data: data),
              let imgData = rep.representation(using: .png, properties: [.compressionFactor : NSNumber(floatLiteral: 1.0)]) else {
            
            Swift.print("\(self) Error Function '\(#function)' Line: \(#line) No tiff rep found for image writing to \(url)")
            return
        }
        
        do {
            try imgData.write(to: url)
        }catch let error {
            Swift.print("\(self) Error Function '\(#function)' Line: \(#line) \(error.localizedDescription)")
        }
    }
}

请帮我做这个。谢谢大家!

下面的话毫无意义。“我正在打开NSOpenPanel并选择一个文件夹或任何目录,然后NSOpenPanel begin方法为我提供所选URL。我想在所选URL上保存图像”如果要保存文件,最好使用
NSSavePanel
。问题是,
NSOpenPanel
的当前配置返回目录的URL,您无法用文件替换目录。错误就是这么说的。至少您必须在目录URL中添加正确的文件名和文件扩展名。我想保存所选路径的多个图像。我与NSSavePanel进行了检查,它无法处理多个文件。有什么建议吗?thanks@ElTomato现在请检查,我更新我的问题。我想保存多个图像,而不是一个。谢谢,请推荐我。我做到了:至少您必须在目录URL中附加一个正确的文件名和文件扩展名