Skip to content

Image from data or UIImage #125

Answered by marcprux
Louis-PAGNIER asked this question in Q&A
Discussion options

You must be logged in to vote

There's unfortunately no SwiftUI.Image(data:) constructor, so if you are wanting to load an image from a Data or byte array, your best bet right now would be to store it to a local URL and then load it with AsyncImage(url:). Something like:

let data: Data = picker.imageData
let url: URL = URL.temporaryDirectory.appendingPathComponent("my-image-\(UUID().uuidString).png")
try data.write(to: url)

// now display the image in a view
AsyncImage(url: url) 

If you don't want to write to the file system, you could also construct a "data:" URL with the base64-encoded bytes, which should also work with AsyncImage.

Let us know if either of these work for you.

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Louis-PAGNIER
Comment options

@marcprux
Comment options

Answer selected by marcprux
@Louis-PAGNIER
Comment options

@marcprux
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants