Hello! I’m struggling to export `androidx.compose....
# compose-ios
a
Hello! I’m struggling to export
androidx.compose.ui.graphics.ImageBitmap
as PNG from the compose app. Any ideas how to do this? Or some links with examples? Ideally I want system’s share bottom sheet to pop up asking user what to do with the image, but I could also settle to some automatic export certain location where user can manually find it later. Thanks!
m
I am using this code to convert the image bitmap to a PNG encoded byte array. You can then save the byte array as a file with Okio for example.
Copy code
expect fun ImageBitmap.encodeToBytes(quality: Int = 100): ByteArray?
Android:
Copy code
actual fun ImageBitmap.encodeToBytes(quality: Int): ByteArray? {
    ByteArrayOutputStream().use { bytes ->
        this.asAndroidBitmap().compress(Bitmap.CompressFormat.PNG, quality, bytes)
        return bytes.toByteArray()
    }
}
All other platforms:
Copy code
actual fun ImageBitmap.encodeToBytes(quality: Int): ByteArray? {
    return Image.makeFromBitmap(this.asSkiaBitmap()).encodeToData(org.jetbrains.skia.EncodedImageFormat.PNG, quality)?.bytes
}
a
Thanks @Michael Paus! This works great on iOS. I found a way to export to a file and I can see the image being saved on Simulator. Any idea how to prompt the system’s share dialog from Kotlin code? Or just to save it on the device so user can manually find from Files iOS app?
m
kodee loving 1
a
Oh man… This worked like a charm! I was looking for libs, but could not find. How do you find this cool libs? Is there like a catalog or something? I’m also looking for kmp lib which can export png to PDF on both Android and iOS. If you have lightning address, send it over in DM so I can send you some sats for a coffee or beer as a sign of gratitude!
m
The official source for such information is now https://klibs.io/ . There is also an older website here https://github.com/mcxiaoke/awesome-kotlin .
Concerning your specific question. I could provide a solution for Desktop and Android but not iOS.
Copy code
pdfbox                         = { module = "org.apache.pdfbox:pdfbox", version.ref = "pdfbox" }
pdfbox-android                 = { module = "com.tom-roush:pdfbox-android", version.ref = "pdfboxAndroid" }
These two variants a very similar and you can share much of your code but of course this is not multiplatform. For iOS one would have to look for something else.
a
Thanks! Links are really helpful!