Home > Back-end >  How would you write an image (e.g. PNG) and save it to your local desktop in SwiftUI?
How would you write an image (e.g. PNG) and save it to your local desktop in SwiftUI?

Time:09-30

I'm generating a UIImage from a View and I'd like to save it to my desktop so I can view the output and see if I'm saving the correct view. What's the process to save an image to my local machine?

I'm not able to find the documentation on how to save it to my desktop.

CodePudding user response:

After rereading your question it looks like you want to save an image made in iOS app to your Mac desktop.

Here is no easy way to achieve it.

If you running app in simulator you can save image in app sandbox container.
You can see content of all containers in

/Users/{USERNAME}/Library/Developer/CoreSimulator/Devices/{long uuid of simulator}/data/Containers

If you running the app on device, easiest way is to save an image in photo library
Saving UIImage to PhotoLibrary Swift 4.2 iOS 12


Assuming that you running MacOS app.

If you talking about Catalyst and UIImage than getting Data from image is as easy as:

image.pngData()

If you meant NSImage, than process is more complex:

let tiffData = image.tiffRepresentation! // don't forget to check for nil
let pngData = NSBitmapImageRep(data: tiffData)?.representation(using: .png, properties: [:])

Then just write data via

data.write(to: url)
  • Related