0

I am trying to save images on a macOS Catalyst app using the below code. It was adopted from SwiftUI Catalyst App in macOS computer - Which path should I use to save an image in my Mac local disk?

However I get the error "The file "abstract-1.jpg" doesn't exist."

I have the image abstract-1 in my Assets.xcassets folder, so I'm not sure why it's not seeing it for this save operation? I've also tried just using item.name without appending the .jpg but it produces the same error. Any ideas? Thanks!

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")
                                        }
phiredrop
  • 182
  • 1
  • 11

1 Answers1

0

Turns out I needed to do two things:

  1. NOT use something like ~/Pictures in my pathURL. This needed to be explicitly declared like @State private var pathURL = "/Users/jimbob/Desktop/"
  2. Change App Sandbox to NO in appname.entitlements

Now to try and get either the user's home directory path, or get a 'save location' dialogue to pop up.

phiredrop
  • 182
  • 1
  • 11