Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 816
JR. Avatar asked Aug 11 '19 06:08

JR.


People also ask

How do I save an image in Core Data?

Name it img and make sure the attribute type is Binary Data, then click on the img attribute and go to Data Model Inspector. Check the box Allows External Storage. By checking this box, Core Data saves a reference to the data which will make for faster access.

How do I save an image?

If you have a touchscreen, long tap to bring up the context menu. Select Save Image As. Choose a location and filename. Select Save and you're done!


1 Answers

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

like image 158
kontiki Avatar answered Sep 19 '22 12:09

kontiki