Hi, I'm trying to download images from web with co...
# compose-ios
s
Hi, I'm trying to download images from web with coil3 and I achieved it in android part of my code but for iOS part I dont know how to convert Bitmaps to real files. Here is my android implementation:
Copy code
@OptIn(ExperimentalCoilApi::class)
actual fun createImage(image: Image, context: PlatformContext): ByteArray? {
    val bitmap = image.asDrawable(context.resources).toBitmap()
    val outputStream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
    return outputStream.toByteArray()
}
in ios:
Copy code
@OptIn(ExperimentalCoilApi::class)
actual fun createImage(image: Image, context: PlatformContext): ByteArray? {
    val bitmap = image.toBitmap()
    val pixels = bitmap.readPixels()
    return pixels
}
its even looking silly I know :)). Can you share if you have any idea ? Thanks in advance. Edit: When i tryout this, it successfully gets the image info like width and height but when i try to draw this with coil async image composable then it throws this :
m
On iOS you could use this:
Copy code
fun ImageBitmap.encodeToBytes(format: org.jetbrains.skia.EncodedImageFormat, quality: Int): ByteArray? {
    return Image.makeFromBitmap(this.asSkiaBitmap()).encodeToData(skiaFormat, quality)?.bytes
}
s
Ah that worked like a charm, thank you
👍 1