文件“xxx”无法保存在文件夹“xxx”中 - Not Saving NSImage of NSOpenPanel selected url in swift macos

问题描述

我是 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()
            }
        }
}

我测试了 writePNG 代码,它工作正常。

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

请帮我解决这个问题。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)