dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

Support for data:image url? #119

Closed tonyxiao closed 3 years ago

tonyxiao commented 3 years ago

Does url-image support data image url of the form?

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAgAElEQVR4XuxdB5QUxRa9LEvOOWeVJIIEBckCSg5GQEAkg0gQJAosKDlIkAwSJKoIiCBIEEmSROATVZCcWXJe4J9bs7PMxume7pnpmXnvHA7oVle9ul1bt6teigcRQUAQEAQEAUHABQTiufCMPCIICAKCgCAgCEAIRBaBICAICAKCgEsICIG4BJs8JAgIAoKAICAEImtAEBAEBAFBwCUEhEBcgk0eEgQEAUFAEBACkTUgCAgCgoAg4BICQiAuwSYPCQKCgCAgCAiByBoQBAQBQUAQcAkBIRCXYJOHfBCBBADSAkgNICWAFACSA0gGIAmAxAASAWC7YADxAQSFz...?

tonyxiao commented 3 years ago

Just figured out that it's pretty trivial to do in SwiftUI itself without needing url-image. Though for completeness I wish url image supported it.

func imageFromBase64(_ base64: String) -> UIImage? {
    if let url = URL(string: base64) {
        if let data = try? Data(contentsOf: url) {
            return UIImage(data: data)
        }
    }
    return nil
}

struct RootView: View {

    var body: some View {
        Image(uiImage: imageFromBase64(imageUrlInBase64)!)
    }
}
dmytro-anokhin commented 3 years ago

TIL I didn’t know about this format and that Cocoa supports it so seamlessly. Surely I’m gonna explore how to include it in URLImage.

dmytro-anokhin commented 3 years ago

Hey, this works out of the box. I'm using standard tools (URLSession, Image I/O) so I guess this shouldn't be a surprise :) Anyway, I added a unit test to make sure this will continue working.