Skip to content

Used to load network and local pictures, supports caching, apply to compose-multiplatform(Desktop).

License

Notifications You must be signed in to change notification settings

ltttttttttttt/load-the-image

Repository files navigation

简体中文

load-the-image

load-the-image Apply to compose-jb(desktop), Used to load network and local pictures.

Example:

Example

Mode of use

Use the code load image with network, file, resources and more

//url="https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg"
Image(rememberImagePainter(url), "")

Add to your project

Step 1.Root dir, build.gradle.kts add:

buildscript {
    repositories {
        maven("https://jitpack.io")//this
        ...
    }
}

allprojects {
    repositories {
        maven("https://jitpack.io")//this
        ...
    }
}

Step 2.Your compose-desktop dir, build.gradle.kts add:

version =

kotlin {
    sourceSets {
        val jvmMain by getting {
            dependencies {
                ...
                implementation("com.github.ltttttttttttt:load-the-image:$version")//this
            }
        }
    }
}

Step 3.Recommendation: uniformly configure the failure graph path displayed when loading fails

fun main() {
    LoadTheImageManager.defaultErrorImagePath = "drawable-xxhdpi/load_error.jpeg"//this
    application {
        Window(onCloseRequest = ::exitApplication) {
            MaterialTheme {
                Image(rememberImagePainter("https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg"),"")
            }
        }
    }
}

ps:Reference of Resource location, you can customize

Resource location

Step 4.Recommendation: set the version of Compose-jb(desktop) to 1.1.1 or above

According to the feedback, it is found that there is a bug in the 1.1.0 version of compose JB, which will not start

Custom configuration

1.Configure placeholder and error image

You can use the default parameters in the method

placeholder

Usage:

rememberImagePainter(url, /*the placeholder path*/)

Or:

    rememberImagePainter(DataToBeLoaded(url).apply {
        placeholderResource = /*the placeholder path*/
        errorImagePath = /*the error resource path*/
    })

2.Modify memory cache size

LoadTheImageManager.memoryCache = ImageLruMemoryCache(/*max memory cache size*/)

ps:Default memory cache size:maxOf(50MB, 1% of total memory)

You can customize:

LoadTheImageManager.memoryCache = /*your class*/

3.Modify file cache location

LoadTheImageManager.fileCache = ImageFileCache(File("C://test_dir"))

Or:

LoadTheImageManager.fileCache = /*your class*/

ps:Default file cache location: user\Pictures\LoadTheImageCache

4.Modify http loader

LoadTheImageManager.httpLoader = /*your class*/

5.Load-the-image supports multiple formats, And it can be expanded by itself

function formats

Custom reference below:

One.Implement your class:

custom_load-the-image

Two.Configure:

LoadTheImageManager.loadTheImage.add(ByteArrayLoadTheImage()/*your class*/)

Three.Use:

rememberImagePainter(DataToBeLoaded(byteArrayOf()))//Better seal it

If you use compose(Kotlin Multiplatform), You can refer to the example.

version =

Your common dir, build.gradle.kts add:

val desktopMain by getting{
	dependencies {
		implementation 'com.github.ltttttttttttt:load-the-image:$version'
	}
}

commonMain add function:

@Composable
expect fun rememberImagePainter(url: String): Painter

androidMain add function(and other target):

@Composable
actual fun rememberImagePainter(url: String): Painter =
    coil.compose.rememberImagePainter(data = url)

desktopMain add function:

@Composable
actual fun rememberImagePainter(url: String): Painter =
    com.lt.load_the_image.rememberImagePainter(url)
    

Use the code load image with network and file and resources

Image(rememberImagePainter(/*url*/"https://img.zcool.cn/community/017e625e57415ea801216518c25819.jpg@1280w_1l_2o_100sh.jpg","")