Coil - Coroutine Image Loader

Coil - Coroutine Image Loader

 
 
 
 
An image loading library for Android backed by Kotlin Coroutines. Coil is:
  • Fast: Coil performs a number of optimizations including memory and disk caching, downsampling the image in memory, automatically pausing/cancelling requests, and more.
  • Lightweight: Coil adds ~2000 methods to your APK (for apps that already use OkHttp and Coroutines), which is comparable to Picasso and significantly less than Glide and Fresco.
  • Easy to use: Coil's API leverages Kotlin's language features for simplicity and minimal boilerplate.
  • Modern: Coil is Kotlin-first and uses modern libraries including Coroutines, OkHttp, Okio, and AndroidX Lifecycles.
Coil is an acronym for: Coroutine Image Loader.
 
 
Coil is available on mavenCentral().
implementation("io.coil-kt:coil:2.6.0")
Quick Start
ImageViews
To load an image into an ImageView, use the load extension function:
// URL imageView.load("https://example.com/image.jpg") // File imageView.load(File("/path/to/image.jpg")) // And more...
Requests can be configured with an optional trailing lambda:
imageView.load("https://example.com/image.jpg") { crossfade(true) placeholder(R.drawable.image) transformations(CircleCropTransformation()) }
Image Loaders
Both imageView.load and AsyncImage use the singleton ImageLoader to execute image requests. The singleton ImageLoader can be accessed using a Context extension function:
val imageLoader = context.imageLoader
ImageLoaders are designed to be shareable and are most efficient when you create a single instance and share it throughout your app. That said, you can also create your own ImageLoader instance(s):
val imageLoader = ImageLoader(context)
If you do not want the singleton ImageLoader, depend on io.coil-kt:coil-base instead of io.coil-kt:coil.
Requests
To load an image into a custom target, enqueue an ImageRequest:
val request = ImageRequest.Builder(context) .data("https://example.com/image.jpg") .target { drawable -> // Handle the result. } .build() val disposable = imageLoader.enqueue(request)
To load an image imperatively, execute an ImageRequest:
val request = ImageRequest.Builder(context) .data("https://example.com/image.jpg") .build() val drawable = imageLoader.execute(request).drawable
 
Check out Coil's full documentation here.