Skip to main content

How to Load Images in Kotlin Multiplatform (KMM) Using Coil

How to Load Images in Kotlin Multiplatform (KMM) Using Coil

How to Load Images in Kotlin Multiplatform (KMM) Using Coil

FeatureDescription
Library UsedCoil (Image Loading Library)
Platforms SupportedAndroid, iOS, Web, Desktop (via KMM)
Key BenefitsFast, lightweight, easy integration
ImplementationKotlin Multiplatform with Jetpack Compose & SwiftUI
Use CasesDisplaying remote/local images, caching, placeholders

Introduction

Kotlin Multiplatform (KMM) allows developers to build cross-platform apps for Android, iOS, Web, and Desktop with a single codebase. One challenge in KMM is image loading since different platforms handle images differently.

Solution: The Coil image loading library, originally built for Android, now supports Kotlin Multiplatform and integrates smoothly with Jetpack Compose.

Why Use Coil in KMM?

  • ✅ Works on Android, iOS, Web, and Desktop
  • ✅ Easy integration with Jetpack Compose & SwiftUI
  • ✅ Uses coroutines for fast async loading
  • ✅ Supports caching, SVGs, GIFs, and transformations

Setup Coil in a Kotlin Multiplatform Project

1. Add Coil Dependency

In your shared module (build.gradle.kts), add:

dependencies {
    implementation("io.coil-kt:coil-compose:2.2.2") // Latest version
}

2. Initialize Coil in KMM

For Android & Compose Multiplatform, Coil works out-of-the-box. But for iOS, use the expect/actual pattern.

📌 Expect Declaration (Common Code - ImageLoader.kt)

expect fun getImageLoader(): ImageLoader

🔹 Actual Implementation (Android - ImageLoader.android.kt)

actual fun getImageLoader(): ImageLoader {
    return ImageLoader.Builder(context)
        .crossfade(true)
        .build()
}

🍏 Actual Implementation (iOS - ImageLoader.ios.kt)

actual fun getImageLoader(): ImageLoader {
    return ImageLoader.Builder()
        .build()
}

Using Coil in Jetpack Compose (Android & Desktop)

@Composable
fun LoadImage(url: String) {
    Image(
        painter = rememberAsyncImagePainter(url),
        contentDescription = "Loaded Image",
        modifier = Modifier.size(150.dp)
    )
}

Using Coil in SwiftUI (iOS)

import SwiftUI
import SDWebImageSwiftUI

struct LoadImage: View {
    var url: String
    var body: some View {
        WebImage(url: URL(string: url))
            .resizable()
            .scaledToFit()
            .frame(width: 150, height: 150)
    }
}

Reusable Image Loader for KMM

Create a reusable function to load images across platforms:

fun loadImage(url: String): Painter {
    return rememberAsyncImagePainter(url)
}

Conclusion

With Kotlin Multiplatform and Coil, you can efficiently load images across multiple platforms using shared code. This improves performance, reduces code duplication, and simplifies development.

Comments

Popular posts from this blog

How to Integrate Razorpay in Kotlin Multiplatform Mobile (KMM) for WebApp JS Browser Target

Integrate Razorpay in KMM for WebApp JS Browser Target How to Integrate Razorpay in Kotlin Multiplatform Mobile (KMM) for WebApp JS Browser Target If you're working with Kotlin Multiplatform Mobile (KMM) and want to integrate Razorpay for payments, you might find plenty of documentation for Android and iOS. However, integrating Razorpay into the WebApp JS Browser target isn't as straightforward. In this blog post, I'll guide you through the steps to get Razorpay working in your KMM project for the WebApp JS Browser target. Why Kotlin Multiplatform Mobile (KMM)? KMM allows you to share business logic across Android, iOS, Web, and other platforms, making it easier to maintain a single codebase. But, when it comes to platform-specific features like payments, you need to implement certain functionality separately for each platform. Integrating Razorpay in the JS Browser Target To integrate Razo...

Understanding Kotlin Yarn Lock in KMM Projects

Kotlin Yarn Lock in KMM Projects Understanding Kotlin Yarn Lock in KMM Projects As a Kotlin Multiplatform Mobile (KMM) developer, working with JavaScript (JS) targets is a crucial aspect when building truly cross-platform applications. In this post, we’ll dive into the importance of the yarn.lock file in KMM projects, how it functions, and where it fits within your project structure. What is yarn.lock ? The yarn.lock file is a critical component in any KMM project that targets JavaScript, whether it's jsBrowser or jsNode . When managing JavaScript dependencies with Yarn, the yarn.lock file locks down the versions of all installed packages, ensuring that every developer and every environment running your project uses the exact same versions. This consistency is vital for avoiding the dreaded "it works on my machine" problem. Why is yarn.lock Important in KMM? When building a KMM project, you might need to int...

Part 2: Getting Started with Kotlin Multiplatform Mobile (KMM)

Getting Started with Kotlin Multiplatform Mobile (KMM) Getting Started with Kotlin Multiplatform Mobile (KMM) If you're new to Kotlin Multiplatform Mobile, here’s a quick guide to help you get started: Setup Your Development Environment Install the latest version of Android Studio . Install the Kotlin plugin . Set up Xcode on your Mac for iOS development . Create a New KMM Project Use Android Studio's KMM project template to create a new project. Configure the project for both Android and iOS targets. Write Shared Code Start by writing common code that can be shared between platforms, such as data models, network logic, and business rules. Here’s a simple example: // Shared code module expect class Platform() { val name: String ...