https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
r

Roger Kreienbühl

03/18/2024, 8:20 AM
I am using
Copy code
Image.makeFromEncoded(this).toComposeImageBitmap()
Is there a way to get a mutable ImageBitmap instead of immutable one? I try to draw this on a Canvas and always get the error
kotlin.IllegalArgumentException: Cannot draw on immutable ImageBitmap
m

mohamed rejeb

03/18/2024, 8:31 AM
You also create your own custom Image.toBitmap method to create a mutable Bitmap, follow the same Compose internal implementation and remove the line where they are setting the bitmap to be immutable. And then you will have something like this:
Copy code
image.toMutableBitmap().asComposeImageBitmap()
r

Roger Kreienbühl

03/18/2024, 9:40 AM
Thanks for the answer. this seems to work. I tried to work with
Bitmap
directly, but android Bitmap and skia Bitmap aren’t compatible as type when using in common code (I am relatively new to kotlin multiplatform).
m

mohamed rejeb

03/18/2024, 9:52 AM
Nice, using
asComposeImageBitmap
on the Bitmap should keep it mutable so you can have the compose ImageBitmap that you can use in common. Let me know if you still have any issues.
3 Views