Skip to content

Commit

Permalink
优化加载和缓存性能,修改默认HttpLoader为OkHttpLoader来提高图片请求性能
Browse files Browse the repository at this point in the history
  • Loading branch information
ltttttttttttt committed Apr 23, 2024
1 parent f589cd4 commit b0df20b
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 26 deletions.
1 change: 1 addition & 0 deletions load-the-image/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation(compose.ui)
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
implementation("com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.2")
implementation("com.squareup.okhttp3:okhttp:4.9.1")
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import com.lt.load_the_image.cache.ImageCache
import com.lt.load_the_image.cache.ImageFileCache
import com.lt.load_the_image.cache.ImageLruMemoryCache
import com.lt.load_the_image.loader.*
import com.lt.load_the_image.loader.http.HttpLoader
import com.lt.load_the_image.loader.http.HttpURLConnectionLoader
import com.lt.load_the_image.loader.http.OkHttpLoader
import com.lt.load_the_image.loader.image.*
import com.lt.load_the_image.painter.DefaultPainterCreator
import com.lt.load_the_image.painter.EmptyImagePainter
import com.lt.load_the_image.painter.PainterCreator
Expand All @@ -49,7 +53,7 @@ object LoadTheImageManager {
/**
* Load network resource to byteArray
*/
var httpLoader: HttpLoader = HttpURLConnectionLoader()
var httpLoader: HttpLoader = OkHttpLoader()

/**
* Create Painter from ByteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ open class ImageLruMemoryCache(
cacheMap[url] = t
cacheSize.getAndAdd(t.size.toLong())
while (cacheSize.get() > maxMemorySize && cacheMap.isNotEmpty()) {
val byteArray = cacheMap.remove(cacheMap.keys.first())
val byteArray = cacheMap.remove(cacheMap.keys.firstOrNull() ?: run {
cacheSize.set(0)
return
})
cacheSize.getAndAdd(-(byteArray?.size ?: 0).toLong())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.http

/**
* creator: lt 2022/4/8 [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.http

import com.lt.load_the_image.util.println
import java.net.URL
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lt.load_the_image.loader.http

import com.lt.load_the_image.util.println
import okhttp3.OkHttpClient
import okhttp3.Request

/**
* creator: lt 2022/4/8 [email protected]
* effect : Use OkHttp load url
* warning:
*/
open class OkHttpLoader(
private val okHttpClient: OkHttpClient = OkHttpClient()
) : HttpLoader {

override fun load(url: String): ByteArray? {
return try {
val build = Request.Builder()
.url(url)
.build()
okHttpClient.newCall(build).execute().body?.bytes()
} catch (e: Exception) {
e.println()
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asComposeImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import org.jetbrains.skia.Bitmap

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.Painter
import com.lt.load_the_image.LoadTheImageManager
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage

/**
* creator: lt 2022/6/1 [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.Painter
import com.lt.load_the_image.LoadTheImageManager
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import com.lt.load_the_image.painter.AsyncImagePainter
import com.lt.load_the_image.util.println
import kotlinx.coroutines.Dispatchers
Expand All @@ -44,21 +46,24 @@ open class FileLoadTheImage : LoadTheImage {
val painter = remember(file.absolutePath) { AsyncImagePainter() }
LaunchedEffect(file.absolutePath) {
withContext(Dispatchers.IO) {
val byteArray = LoadTheImageManager.memoryCache.getCache(file.absolutePath) ?: try {
val byteArray = file.readBytes()
if (byteArray.isEmpty()) {
var byteArray: ByteArray? = LoadTheImageManager.memoryCache.getCache(file.absolutePath)
if (byteArray == null) {
try {
val array = file.readBytes()
if (array.isEmpty()) {
painter.imageBitmap.value =
LoadTheImageManager.loadResourceImageBitmap(data.errorImagePath)
return@withContext
}
LoadTheImageManager.memoryCache.saveCache(file.absolutePath, array)
byteArray = array
} catch (e: Exception) {
e.println()
painter.imageBitmap.value =
LoadTheImageManager.loadResourceImageBitmap(data.errorImagePath)
return@withContext
}
byteArray
} catch (e: Exception) {
e.println()
painter.imageBitmap.value =
LoadTheImageManager.loadResourceImageBitmap(data.errorImagePath)
return@withContext
}
LoadTheImageManager.memoryCache.saveCache(file.absolutePath, byteArray)
painter.imageBitmap.value =
try {
LoadTheImageManager.painterCreator.createImageBitmap(byteArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toComposeImageBitmap
import com.lt.load_the_image.LoadTheImageManager
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import com.lt.load_the_image.painter.AsyncImagePainter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -41,9 +43,14 @@ open class HttpLoadTheImage : LoadTheImage {
LaunchedEffect(url) {
withContext(Dispatchers.IO) {
//Use cache
var byteArray =
LoadTheImageManager.memoryCache.getCache(url)
?: LoadTheImageManager.fileCache.getCache(url)
var byteArray: ByteArray? = LoadTheImageManager.memoryCache.getCache(url)
if (byteArray == null) {
byteArray = LoadTheImageManager.fileCache.getCache(url)
if (byteArray != null) {
LoadTheImageManager.memoryCache.saveCache(url, byteArray)
}
}
//Http load
if (byteArray == null) {
val placeholderResource = data.placeholderResource
if (placeholderResource.isNotEmpty())
Expand All @@ -55,9 +62,9 @@ open class HttpLoadTheImage : LoadTheImage {
painter.imageBitmap.value = LoadTheImageManager.createErrorImageBitmap(data)
return@withContext
}
LoadTheImageManager.memoryCache.saveCache(url, byteArray)
LoadTheImageManager.fileCache.saveCache(url, byteArray)
}
LoadTheImageManager.memoryCache.saveCache(url, byteArray)
LoadTheImageManager.fileCache.saveCache(url, byteArray)
val imageBitmap = Image.makeFromEncoded(byteArray).toComposeImageBitmap()
painter.imageBitmap.value = imageBitmap
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.toComposeImageBitmap
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import org.jetbrains.skia.Image

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.loadImageBitmap
import com.lt.load_the_image.LoadTheImageManager
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import com.lt.load_the_image.painter.AsyncImagePainter
import com.lt.load_the_image.util.println
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
* limitations under the License.
*/

package com.lt.load_the_image.loader
package com.lt.load_the_image.loader.image

import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.ClassLoaderResourceLoader
import androidx.compose.ui.res.painterResource
import com.lt.load_the_image.loader.DataToBeLoaded
import com.lt.load_the_image.loader.LoadTheImage
import com.lt.load_the_image.util.println

/**
Expand Down

0 comments on commit b0df20b

Please sign in to comment.