微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决文件“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 举报,一经查实,本站将立刻删除。