https://kotlinlang.org logo
Title
s

Stefan Oltmann

04/18/2023, 9:44 AM
I'm looking for an easy way to get a
androidx.compose.ui.graphics.painter.Painter
for my SVG & bitmap resource images. For JVM we have
loadXmlImageVector()
and
BitmapPainter
, which are not available for Compose for iOS. I have them in a folder, but I wouldn't mind (for now) to further use the
Assets.xcassets
as SwiftUI requires me to. I looked at https://github.com/Kotlin/kmm-production-sample/tree/compose-app which uses https://github.com/Skeptick/libres to do that, but this unfortunately requires CocoaPods, which I don't want in my project setup. What are other options to use SVG & Bitmaps for my Icons with Compose for iOS?
The only thing that comes to my mind is
Image.makeFromEncoded(nsData.toByteArray()).toComposeImageBitmap()
, but I would like to continue using SVGs for my icons.
Ok, as far as I can see also the official solution requires CocoaPods and the way to avoid this is not documented. Does somebody figured out how to do it and can provide an example? https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1681737215033999?thread_ts=1681735276.820739&cid=C0346LWVBJ4
m

Michael Paus

04/18/2023, 1:14 PM
For Bitmaps you can just use the new (since 1.4.0)
org.jetbrains.compose.resources.painterResource
which you can get via
implementation(compose.components.resources)
and for SVG a modified version of
androidx.compose.ui.res.SVGPainter and
androidx.compose.ui.graphics.vector.DrawCache
from the desktop implementation. It is actually quite simple because both desktop and iOS are based on Skia. I just had to modify the loader so that it can read from a ByteArray instead of a Java stream. You can read the raw ByteArray as a resource via the resource package mentioned above.
s

Stefan Oltmann

04/18/2023, 1:23 PM
Thanks. Yes, I play around with that right now. I try to figure out what CocoaPods actually does in the mix and if I this can be done by a Gradle task.
a

alex009

04/18/2023, 1:40 PM
you can use moko-resources without cocoapods
s

Stefan Oltmann

04/18/2023, 1:40 PM
This is already compatible with Compose for iOS?
s

Stefan Oltmann

04/18/2023, 1:41 PM
Nice, I will check that out 🙂 🙏
a

ayodele

04/18/2023, 10:35 PM
I had issues loading SVGs using
painterResource