bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.67k stars 6.12k forks source link

Unable to cache to disk scaled and transformed image (RoundedCorners) with DiskCacheStrategy.RESOURCE #5270

Closed bubbleguuum closed 1 year ago

bubbleguuum commented 1 year ago

Version: 4.16.0

My app loads http images that can eventually be large, to put them into thumbnail ImageView with round corners. I always want to cache to disk the final scaled image with the transformation (round corners) applied. From what I understand, this is what DiskCacheStrategy.RESOURCE should do, but it does not: if there is a transformation specified if always cache to disk the original image. If I omit the transformation, it properly caches to disk the scaled (to ImageView dimension) image.

In my AppGlideModule, I have:

  builder.setDefaultRequestOptions(
                RequestOptions.bitmapTransform(new RoundedCorners(someSizeInPixels)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE));

The actual request is nothing special and looks like:

    RequestBuilder<Drawable> builder = Glide.with(imageView.getContext())
                    .load(glideUrl)
                    .override(someSizeInPixels)
                    .error(missingDrawable)
                    .placeholder(missingDrawable)
                    .listener(new RequestListener<>() {
                        ...
                    })
                                        .into(imageView);

So to summarize, with the presence of the RoundedCorners transform, DiskCacheStrategy.RESOURCE cache to disk the (potentially very big) original image and behaves like DiskCacheStrategy.DATA. If the transform is not specified (either removed from the AppGlideModule or discarded with dontTransform() passed to the request builder), the scaled image is cached to disk (without round corners obviously).

What I need is the scaled image with rounded corner to be cached to disk. How can I do that ?

bubbleguuum commented 1 year ago

It works at intended and another PEBCAK case .

I was confused because when the RoundedCorners transformation is used, cached files are saved as PNG (because of the new transparent pixels in the corners) resulting in much bigger files that I though were original files.

It is a bit unfortunate that rounded corners results in much bigger cached files: it increases storage usage by quite a bit, cause more disk I/O at loading time and PNG seems to be generally slower than JPG decoding. There has to be a more optimized way for a few pixels shaved off corners...