swift - 文件“xxx”无法保存在文件夹“xxx”中 - 不保存 NSOpenPanel 的 NSImage 在 swift macos 中选择的 url
问题描述
我是 MACOS Development 的新手,正在制作一个应用程序。在我的应用程序中,我打开 NSOpenPanel 并选择一个文件夹或任何目录(如下载、文档),然后 NSOPenPanel 开始方法给我选择的 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)")
}
}
}
请帮我解决一下这个。谢谢你!
解决方案
推荐阅读
- azure-billing-api - Azure 中国计费报告 API 端点
- python-3.x - java.util.NoSuchElementException:找不到密钥:_PYSPARK_DRIVER_CALLBACK_HOST
- python - array.copy() 与 numpy.copy(array) 之间的区别
- amazon-web-services - 如何修复 API 从我的 AWS Kubernetes pod 中看到的 IP 地址
- python - 如何在 Python 中查找 .txt 文件中某一列的总和?
- python - 写入输出时,标头不充当标头。Python
- python - Use a variable number of conditions to select rows whose column value equals some value
- python - 使用最低有效位在图像中隐藏图像
- ansible - 当 inventory_hostname 位于变量树的顶部时,Ansible Jinja2 问题迭代列表
- autohotkey - 如何在自动热键中转义 +