Taking a look at Coil. An image loading library for Kotlin on Android. 
Tags: kotlin, android, java
Published: 2020-01-02 16:11:45

Happy New Year everyone! If you're reading this, it probably means that its time to get back to the code editor. I recently discovered Coil, an image loading library, I thought I would explore it a little bit and share what I found. _Ehem_,

#Coil: What is it?

Well, according to their [documentation](https://coil-kt.github.io/coil/) Coil stands for **Co**routine **I**mage **L**oader and it is an image loading library backed by Kotlin Coroutines. If you aren't familiar with Kotlin Coroutines, then fear not, these are just lightweight threads. Also knowledge of Coroutines isn't required to use this library so I will not be diving into the intricate details of Kotlin Coroutines.

## But do we really need another image loading library?

You are probably wondering if we really need another image loading library in a world full of Glide and Picasso or Universal Image Loader -  _for those of you who have been developing on the Android platform for a while now_😉. However two things caught my attention with regards to Coil and that is:

1. It is smaller in size compared to Glide, which was my image loading library of choice. And,
2. It is surprisingly very easy to use. You shall see further down in this article, that the library is very idiomatic, which is what Kotlin is all about and that is awesome. 

#Okay I'm sold, what next?

In order to use Coil in your project you need to have your project already using AndroidX. It must also compile with SDK level 29 as well as use Java 8. 

If you satisfy the above per-requisites then you need to add in the following line to your application level dependencies in your `build.gradle` file before you can use the library:

```kotlin
implementation("io.coil-kt:coil:0.9.1")
```

This will include the default artifact with the `Coil` singleton. Just like that, you are ready to start easier and faster Image Loading in your Android applications.

##Loading an Image

To load an image to an `ImageView`, Coil offers a nifty extension function called `load()`.

**Below is a code snippet of the library in action:**

```kotlin
//Loading from a URL
imageView.load("https://via.placeholder.com/600/92c952")

//Loading from an image drawable
imageView.load(R.drawable.some_image_placeholder)

// Loading from a file
imageView.load(File("/path/to/some_image_placeholder.png"))
```

Walla! It's really that simple. _You can't make this stuff up!_

Under the hood, the `load()` function does a lot of things. Take for instance loading from a URL: 

* Coil creates a `RequestDisposable` object, which is basically an object responsible for connecting to the network.
* The image specified in the URL is requested for over the network and the result is loaded onto a `target`.
* In this case the instance of the `ImageView`is the intended `target`(_and hence the beauty of extension functions_).

#Let's explore a little more...

At this point, you are probably wondering how to make this library do certain things like configure a placeholder or maybe apply a transformation to circle crop your image. 

Well its really easy. You just add in your configurations to a trailing lambda after the `load()` extension function. 

**Observe:**

```kotlin
//Applying image loading with configurations
imageView.load("https://via.placeholder.com/600/92c952") {
    crossfade(true)
    placeholder(R.drawable.placeholder_image)
    transformations(CircleCropTransformation())
}
``` 

Very simple!.

As if that wasn't enough, Coil supports loading to custom targets.

Using the `load()` extension function from the `Coil` singleton, you just have something like:


```kotlin
//Custom targets
Coil.load("https://via.placeholder.com/600/92c952"){
    target{ drawable ->
        //Do something with result e.g apply a filter to greyscale image.
    }
}
```

You can even listen in for specific events like `onSuccess`, `onStart`, `onError` and `onCancel`, however I will not be getting into these in this article. You can let me know if you want me to dive deeper. And I will create a **Coil party tricks** 2 part article touching every aspect of the library I can find.

##A little bit of more information on Transformations

The Coil library provides 4 transformations out of the box namely,

* `BlurTransformation` - Applies a Gaussian Blur.
* `CircleCropTransformation` - Crops and centers the image into a circle.
* `GrayscaleTransformation` - Shades the image into grayscale.
* `RoundedCornersTransformation` - Crops the image to fit the target's dimensions and rounds the corners of the image.

##Other cool things to note 😎

There are a few cool other things to note about the Coil library.

1. Its support for extensions is massive and this allows for developers to support more file types in their projects to suit their needs.
2. Its has features which makes testing easier. You can basically mock instances of the `ImageLoader`object as it is an interface. 
3. No annotation processing! And this is a big one. This means faster builds in your project in case you were using Glide, you know how important this is. They use Kotlin's extension functions feature.

#In Conclusion

Coil is a pretty neat image loading library for Android. I am in no ways associated with Instacart however I am an advocate for cleaner code and I have always been lazy. I will 💜 anything that makes my life easier and Coil is that thing, atleast for image loading lol!

Please go forth and give it a try, _maybe even migrate from your current Image Loading library._

If you would like to know more I have included a few links that may be use to you below:

* [Introducing Coil: Kotlin-first image loading on Android](https://tech.instacart.com/introducing-coil-kotlin-first-image-loading-on-android-f0fdc7a2a99e)
* [Coil Documentation](https://coil-kt.github.io/coil/)
* [Coil Github Project](https://github.com/coil-kt/coil) - Browse through the code. 


![That's all folks](https://media.giphy.com/media/upg0i1m4DLe5q/giphy.gif)

Feel free to follow me on [Twitter](https://twitter.com/KudzieChase). I'm always up for a discussion.