Skip to content

UI lag when converting bitmap to bytearray #157

@BoikoIlya

Description

@BoikoIlya

This is IOS part of kmp project. My ui is lagging when I convert bitmap to bytearray

` val originalBitmap = imageBitmap.asSkiaBitmap()

val width: Int
val height: Int

when (aspectRatio) {
    "9:16" -> {
        height = 3840
        width = 2160
    }
    "1:1" -> {
        height = 3840
        width = 3840
    }
    else -> {
        width = originalBitmap.width
        height = originalBitmap.height
    }
}

val scaledBitmap = Bitmap().apply {
    allocN32Pixels(width, height)
}

val timeDraw = measureTime {
    val canvas = Canvas(scaledBitmap)
    val srcRect = Rect.makeWH(originalBitmap.width.toFloat(), originalBitmap.height.toFloat())
    val dstRect = Rect.makeWH(width.toFloat(), height.toFloat())
    canvas.drawImageRect(Image.makeFromBitmap(originalBitmap), srcRect, dstRect)
}.inWholeMilliseconds
println("Drawing bitmap took: $timeDraw ms")

val byteArray = withContext(Dispatchers.Default) {
    var result: ByteArray? = null
    val timeEncode = measureTime {
       result = Image.makeFromBitmap(scaledBitmap).encodeToData(EncodedImageFormat.PNG)?.bytes ?: ByteArray(1)
    }.inWholeMilliseconds
    println("Encoding image took: $timeEncode ms")
    result!!
}`

I launch it in coroutine on default dispatcher but it still lag my ui. I have investigated that problem cause that line:
Image.makeFromBitmap(scaledBitmap).encodeToData(EncodedImageFormat.PNG)?.bytes

I use PNG format and it leads to 2 sec ui delay, but with JPEG it works very fast with no delay. please help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions