qdsfdhvh / compose-imageloader

Compose Image library for Kotlin Multiplatform.

Home Page:https://qdsfdhvh.github.io/compose-imageloader/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use with local file path

paulocoutinhox opened this issue · comments

Hi,

How to use with local file path in async way?

Example:

/home/paulo/Downloads/image.jpg

Thanks.

There is a way to convert String to okio.Path, which currently supports by OkioPathFetcher, but this not every platform supports.

val file = "/home/paulo/Downloads/image.jpg"
val path = file.toOkioPath()
val imageRequest = ImageRequest { data(path) }

expect fun String.toOkioPath(): okio.Path?

// jvm
import okio.Path.Companion.toPath
actual fun String.toOkioPath(): okio.Path? {
    return toPath()
}

// ios
import okio.Path.Companion.toPath
actual fun String.toOkioPath(): okio.Path? {
    return toPath()
}

// js & wasmJs
actual fun String.toOkioPath(): okio.Path? {
    return null // maybe not support, i don't know about it.
}
commented

@qdsfdhvh How to load "commonMain/resources/xxx.gif" under resource files?
I tried toPath() he failed.
MR resource id generation has not been used yet, images are loaded with painterResource("xxx.png") this way.
Thanks brother!

It is better to use compose-resources for 'commonMain/resources', here is a demo;

Qucik Start:

file: gradle.properties

# https://github.com/JetBrains/compose-multiplatform/pull/3961
compose.resources.always.generate.accessors=true

file: build.gradle.kts

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation(compose.components.resources)
            }
        }
    }
}

then you can use like this:

Image(
  painter = painterResource(Res.drawable.xx_gif)
)

however, this solution supports a limited number of formats, and you can also use the following extensions:

dependencies {
     implementation(compose.components.resources)
+    api("io.github.qdsfdhvh:image-loader-extension-compose-resources:1.7.8")
}
Image(
  painter = rememberImagePainter(Res.drawable.xx_gif)
)

this is my demo

By the way, compose.components.resources is still experimental, the api may change later.