macOS计算机中的SwiftUI Catalyst应用-我应使用哪个路径将图像保存到Mac本地磁盘中?

问题描述 投票:0回答:1

我有这个简单的代码,我在macOS中与Catalyst一起运行:

struct ContentView: View {

    @State private var pathURL = "/Users/cesare/Desktop/Test"

    var body: some View {
        VStack {
            TextField("/Users/cesare/Desktop/Test", text: $pathURL)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .padding()
            Button(action: {
                let fileUrl = URL(fileURLWithPath: self.pathURL, isDirectory: true)

                let nameImage = "ImageTest" + ".jpg"
                let fileUrlWithName = fileUrl.appendingPathComponent(nameImage)

                let imageData = UIImage(named: "2020-03-05_11-19-22_5")?.jpegData(compressionQuality: 1)
                do {
                    try imageData!.write(to: fileUrlWithName)
                } catch {
                    print("** saveImageData error: \(error.localizedDescription)")
                }
            }) {
                Text("Save Image")
            }
        }
    }
}

Assets.xcassets中放入2020-03-05_11-19-22_5图像。当我按Save Image按钮时,出现以下错误:You don’t have permission to save the file “ImageTest.jpg” in the folder “Test”.为什么我没有在桌面上保存图像的权限?我试图导出“产品”中包含的应用程序,但未得到任何结果。我应该使用哪个路径将图像保存到Mac本地磁盘中?

save swiftui catalyst
1个回答
0
投票

要将文档保存在任何文件夹中,需要将App Sandbox设置为NOenter image description here

© www.soinside.com 2019 - 2024. All rights reserved.