DEV Community

Cover image for Load and cache an image from a URL directly into an ImageView using one annotation.
crypticminds
crypticminds

Posted on

Load and cache an image from a URL directly into an ImageView using one annotation.

Images are one of the most cached items in any application. ColdStorage library provides a very easy way to load an image and cache it directly into an ImageView.

Usage :-


 @LoadImage(
        R.id.image_1,
        "https://images.unsplash.com/photo-1549740425-5e9ed4d8cd34?ixlib=rb-1.2.1&w=1000&q=80",
        placeHolder = R.drawable.loading, enableLoadingAnimation = true
    )
    lateinit var imageWithAnimation: ImageView

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.load_image_example)
        Cache.bind(this)
    }

The image view will bind the variable with its resource id and take care of loading the image after downloading it from the URL into it.

Additionally, it can also show a placeholder image and loading animation until the image is downloaded.

Check out the repository for more detailed usage:- https://github.com/crypticminds/ColdStorage

Top comments (0)