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 to KMM and Coil

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. The Coil image loading library, originally built for Android, now supports Kotlin Multiplatform and integrates smoothly with Jetpack Compose, offering a robust solution.

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 a SwiftUI Alternative (iOS)

Although Coil supports iOS, SwiftUI typically uses its own image loading solution like **`AsyncImage`** or a third-party library like **SDWebImageSwiftUI** for more advanced features. Here's a common approach for SwiftUI:

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

Fixing the Razorpay-Pod Issue in Kotlin Multiplatform Mobile (KMM) for iOS

Fixing Razorpay CocoaPod Integration in KMM for iOS Fixing Razorpay CocoaPod Integration in KMM for iOS Kotlin Multiplatform Mobile (KMM) enables developers to share code between Android and iOS. However, integrating certain CocoaPods, such as ** razorpay-pod **, can be challenging due to naming conventions. This guide provides a step-by-step solution to resolve these issues, ensuring a smooth integration of Razorpay's SDK into your KMM iOS application. Understanding the Issue When integrating ** razorpay-pod ** into a KMM project, developers may encounter errors related to unresolved modules. This often stems from the hyphen in the pod's name, which can cause KMM to misinterpret the module structure, leading to import issues. Solution: Explicitly Defining the moduleName Step 1: Update Your Gradle Configuration In your shared module's build.gradle.kts file, add the following code to explic...

Build Once, Run Anywhere: A Simple Weather App using Kotlin Multiplatform (KMM/KMP/CMP)

Simple Weather App using Kotlin Multiplatform (KMM/KMP) Simple Weather App using Kotlin Multiplatform (KMM/KMP) 🚀 In today’s cross-platform app development world, Kotlin Multiplatform (now popularly known as KMP or KMM ) is a game-changer. It allows us to share business logic across Android and iOS without compromising the native feel and performance of the UI. As a passionate mobile developer, I recently built a Simple Weather Application using KMP (Kotlin Multiplatform Project) . This app is a real-world example of how you can target both Android and iOS platforms with a single codebase using: 💻 Jetpack Compose for Android UI 🍎 SwiftUI for iOS UI ⚙️ Shared business logic in Kotlin 🔗 GitHub Project Link 🌟 Why Kotlin Multiplatform (KMP)? Unlike Flutter or React Native, KMP doesn't replace the native UI — instead, it empowers developers to write platform-s...