Get the Drawable from a success request on Compose
See original GitHub issueIs your feature request related to a problem? Please describe.
There’s a discussion on #851 about getting the drawable from the rememberImagePainter
in order to use on other methods (like getting the color palette) without making another request externally.
The best way of doing that would be using the target
from the ImageRequest
. But, as seen on the discussion, it is set internally so it’s not possible to use that on the image request builder inside the rememberImagePainter
val painter = rememberImagePainter(
imageURL,
builder = {
target( // target must be null
onSuccess = {
doStuffWithDrawable(it)
}
)
},
)
Describe the solution you’d like
A solution i see to that would be a callback like the ImageRequest’s target on the compose function, like so:
val painter = rememberImagePainter(
imageURL,
callbacks = {
onSuccess { drawable ->
doStuffWithDrawable(drawable)
}
}
)
The internal target would pipe the the onStart
, onError
and onSuccess
from the internal target to the callbacks on the remember function.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
Top GitHub Comments
@univeous Ah! Sorry I totally got this wrong and it’s not possible to access the bitmap in
BitmapPainter
. Given the other solutions are hacky, I’m going to push 1.3.3 today which expose theDrawable
inImagePainter.state
. That way you’ll be able to get the drawable using:EDIT: Sorry for the delay. I’ve held of rolling out the update temporarily to verify that adding this attribute doesn’t increase recompositions.
This should be easier in 2.0 since
listener
has the image result. The best way to grab the bitmap currently is to use((rememberImagePainter(data).state.painter) as? BitmapPainter).bitmap