Get SwiftUI Image Data

I’m looking for the easiest and most efficient way to convert a SwiftUI Image to Data so that I can store it in SwiftData.

let image: Image

let data: Data = GetImageData(image: image)

How would I implement the GetImageData function above?

I have found examples of how to do so with UIImage but not Image.

A SwiftUI Image is a View and not a image data. You can use ImageRenderer to create an image from SwiftUI views. That way you could extrapolate the cgImage and binary data.

That said, I would like to know what you’re trying to do with the Image perhaps they could be a better approach. Couldn't you cache the binary data before rendering the SwiftUI Image?

Get SwiftUI Image Data
 
 
Q