In a recent code review I had the discussion if my...
# compose
s
In a recent code review I had the discussion if my way to load images in SKIA is wrong. I use to do this:
Copy code
val imageBitmap = makeImageFromByteArray(bytes).toComposeImageBitmap()
The suggestion is to do this instead:
Copy code
val image = makeImageFromByteArray(bytes)

val imageBitmap = image.use {
    it.toComposeImageBitmap()
}
The reason behind that is that the SKIA image will not be closed after the copy process and the native resources not be freed up... That may be one of the issues for memory issues I experienced on iOS. I don't find anything how to properly use
toComposeImageBitmap()
Is is true that the SKIA image behind will not be freed immediately after a method like this completed?
Copy code
fun createImage(bytes: ByteArray) = makeImageFromByteArray(bytes).toComposeImageBitmap()
Is there a way/need to dispose()/close() the ImageBitmap after the Composable disposes?
👀 1
e
not sure what your
makeImageFromByteArray
is, but if it returns a `org.jetbrains.skia.Image`: • the native Skia image will be freed faster if you
close
it • it has a finalizer so it should be eventually freed after GC
✅ 1
s
Yes, sorry, it's a wrapper around Image.makeFromEncoded()
Copy code
try {

    return Image.makeFromEncoded(byteArray)

} catch (ex: IllegalArgumentException) {
    throw ImageCreationException("Image.makeFromEncoded() failed.", ex)
}
Ok, so I can indeed improve memory usage if I close the Skia Image as fast as possible instead of waiting for the GC to do it eventually. The native resources will be released faster then.