Marko Novakovic
05/19/2021, 6:55 AMaccompanist-coil
to load images, how can I get loaded image back? same as you can with Coil
:
val request = ImageRequest.Builder(context)
.data("<https://www.example.com/image.jpg>")
.target { drawable ->
// Handle the result.
}
.build()
or add Target
on Glide?Zach Klippenstein (he/him) [MOD]
05/19/2021, 7:20 AMloadState
on the LoadPainter
returned from `rememberCoilPainter`: when it’s Loaded
, the painter will be ready to draw the image.
https://google.github.io/accompanist/api/imageloading-core/imageloading-core/com.google.accompanist.imageloading/-load-painter/index.html#%5B[…]4529Zach Klippenstein (he/him) [MOD]
05/19/2021, 7:21 AMrememberCoilPainter
also lets you pass a function that customizes the ImageRequest.Builder
, but idk what happens if you try setting a target there, since i assume rememberCoilPainter
sets the target itself.Marko Novakovic
05/19/2021, 7:22 AMImageLoadState.Success
exposes Painter
but how can I get Bitmap
from it?Zach Klippenstein (he/him) [MOD]
05/19/2021, 7:23 AMZach Klippenstein (he/him) [MOD]
05/19/2021, 7:24 AMMarko Novakovic
05/19/2021, 7:24 AMcb
05/19/2021, 8:11 AMMarko Novakovic
05/19/2021, 8:13 AMMarko Novakovic
05/20/2021, 8:19 PM@Composable
fun CoilImage(
modifier: Modifier = Modifier,
url: String,
) {
val coroutineScope = rememberCoroutineScope()
val loader = ImageLoader(LocalContext.current)
var image by remember { mutableStateOf(ImageBitmap(1, 1)) }
val target = remember {
object : Target {
override fun onSuccess(result: Drawable) {
image = result.toBitmap().asImageBitmap()
}
}
}
val request = ImageRequest.Builder(LocalContext.current).run {
data(url)
target(target)
scale(Scale.FILL)
allowHardware(false)
build()
}
LaunchedEffect(url) {
loader.enqueue(request)
}
Image(
modifier = modifier.height(250.dp),
bitmap = image,
contentScale = ContentScale.FillWidth,
contentDescription = null,
)
}