我正在使用coil(version 2.1.0)从URL加载图片。当有网络连接时,图片可以正常加载。然而,在没有网络连接时,图片并没有像我预期的那样从缓存中获取。这是我的一段代码:
class App : Application(), ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(0.25)
.build()
}
.diskCache {
DiskCache.Builder()
.directory(cacheDir.resolve("image_cache"))
.maxSizeBytes(5 * 1024 * 1024)
.build()
}
.build()
}
}
在Compose中:
val context = LocalContext.current
val placeholderImage = R.drawable.ic_placeholder
val imageRequest = ImageRequest.Builder(context)
.data(imageUrl)
.memoryCacheKey(imageUrl)
.diskCacheKey(imageUrl)
.placeholder(placeholderImage)
.error(placeholderImage)
.fallback(placeholderImage)
.diskCachePolicy(CachePolicy.ENABLED)
.memoryCachePolicy(CachePolicy.ENABLED)
.transformations(CircleCropTransformation())
.build()
AsyncImage(
model = imageRequest,
modifier = Modifier.size(64.dp),
contentDescription = null,
imageLoader = context.imageLoader
)
当设备离线时,它只加载占位图像,而不像预期那样从缓存中加载图像。我错过了什么?