How to create Painter from ByteArray (jpg, png for...
# compose-web
p
How to create Painter from ByteArray (jpg, png formats)? Targets: Js, WasmJs
m
Copy code
fun ByteArray.decodeToImageBitmap(): ImageBitmap? {
    return Image.makeFromEncoded(this).toComposeImageBitmap()
}
From the image you can then create a painter.
🙏 1
p
Decoding bytes of PNG format using Image.makeFromEncoded is ok. Trying to decode bytes of SVG format using the same function got exception. Please, advice for some solution for converting SVG to Painter.
m
On all platforms which use Skia (Android is the only one which doesn’t) creating an SVG painter is as simple as that:
Copy code
@OptIn(ExperimentalResourceApi::class)
fun loadSvgPainter(
    encodedData: ByteArray,
    density: Density
): Painter {
    return encodedData.decodeToSvgPainter(density)
}
p
Thanks, Michael!