-
-
Save KatieBarnett/a773aafbbf9d4480752ac87c8e2a6313 to your computer and use it in GitHub Desktop.
Loading image bitmap using Coil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val context = LocalContext.current | |
| var loadedBitmap by remember(imageUrl) { mutableStateOf<Bitmap?>(null) } | |
| LaunchedEffect(imageUrl) { | |
| withContext(Dispatchers.IO) { | |
| val request = ImageRequest.Builder(context).data(imageUrl).apply { | |
| memoryCachePolicy(CachePolicy.DISABLED) | |
| diskCachePolicy(CachePolicy.DISABLED) | |
| }.build() | |
| // Request the image to be loaded and return null if an error has occurred | |
| loadedBitmap = when (val result = context.imageLoader.execute(request)) { | |
| is ErrorResult -> null | |
| is SuccessResult -> result.drawable.toBitmapOrNull() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment