KMP Load Image From HTTP URL in Compose Multiplatform

In today’s tutorial, we learn how to use KMP to load images from HTTP URLs using the CoilImage library for both Android and iOS. Displaying images from HTTP or HTTPS URLs is essential for every mobile and web application because you cannot store all the application’s images in in-app storage. When working with API, a lot of dynamic data, including images, comes from the server. So, we have to load all of these images from HTTP and HTTPS URLs in our app.

KMP Load Image From HTTP URL in Compose Multiplatform

KMP Load Image From HTTP URL in Compose Multiplatform

We are using the CoilImage GitHub library to load images. The library’s page mentions other libraries for loading images, such as GlideImage and Fresco. You can visit its GitHub page HERE.

Setup CoilImage in KMP project:

1. Open your KMP compose multiplatform composeApp -> build.gradle.kt app level file.

 

KMP App level build.gradle.kts file

2. Paste the CoilImage library in commonMain dependencies. I am using the latest version as of the current date, but if you read this tutorial later, you can find the latest version HERE.

3. Now, we have to set Androd compiled SDK as 35. You can skip this step if you have already used the compiled SDK as 35. To set up, please open gradle -> libs.versions.tool file. And set android-compileSdk as 35.

libs.versions.toml file

android-compileSdk = "35"

Now, SYNC and rebuild your project. Then, we are ready to start the coding.

Error Image: I have designed a custom 404 image not found in Photoshop. You can download and use this image in your project without my content. It is free for everyone. After downloading the image, put it in the composeApp -> commonMain -> composeResource -> drawable folderAfter placing the image, rebuild the project. Without rebuilding the project, your image will not be visible as an import.

KMP Load Image From HTTP URL in Compose Multiplatform

CoilImage Integration in code in KMP:

Code explanation:

  1. modifier = Modifier.size().clip(RoundedCornerShape()): Set the rounded HTTP image corner.
  2. imageModel: Pass the complete image URL here in double quotes as String.
  3. imageOptions: Set image alignment within the parent widget and set image scale type.
  4. loading: Show the Circular progress indicator while loading the image.
  5. Failure: Show a custom widget if the image is unavailable, 404 is not found, or something went wrong.

Complete source code for App.kt file:

Screenshots:

KMP Load Image From HTTP URL in Compose Multiplatform

KMP Load Image From HTTP URL in Compose Multiplatform

 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *