Search code examples
swiftswiftuiios13

How can I save a SwiftUI image to disk or get the data out?


I'm trying to save a SwiftUI image (not a UIImage from UIKit) to disk.

I've looked everywhere but could not find any documented information on how to do this. I also cannot see a way to extract the Data from the SwiftUI image.

Can anybody help? Thanks in advance.


Solution

  • With SwiftUI, things work a little differently. What you want to do, cannot be done in that fashion. Instead, you need to look at how the image was created and obtain the image data from the same place that your Image() got it in the first place.

    Also if you need the actual binary data to save it to disk, you need a UIImage (note that I am not saying UIImageView).

    Fortunately, Image can handle UIImage too, check this other question for that: https://stackoverflow.com/a/57028615/7786555