【发布时间】:2021-03-01 16:38:24
【问题描述】:
我正在尝试使用以下代码在 macOS Catalyst 应用程序上保存图像。取自SwiftUI Catalyst App in macOS computer - Which path should I use to save an image in my Mac local disk?
但是我收到错误"The file "abstract-1.jpg" doesn't exist."
我的 Assets.xcassets 文件夹中有图像 abstract-1,所以我不确定为什么在此保存操作中看不到它?我也试过只使用item.name 而不附加 .jpg 但它会产生相同的错误。有任何想法吗?谢谢!
Button(action: {
let fileURL = URL(fileURLWithPath: self.pathURL, isDirectory: true)
let nameImage = item.name + ".jpg"
let fileURLwithName = fileURL.appendingPathComponent(nameImage)
let imageData = UIImage(named: item.name)?.jpegData(compressionQuality: 1)
do {
try imageData!.write(to: fileURLwithName)
} catch {
print("** saveImageData error: \(error.localizedDescription)")
}
}) {
Label("Save to Pictures Directory", systemImage: "square.and.arrow.down")
}
【问题讨论】: