Skip to content

Commit

Permalink
[MBL-1650] Fix crashes with large WEBP images (#2085)
Browse files Browse the repository at this point in the history
* adjust webp loading settings to have a max size based on screen resolution, downsample otherwise

* apply the same fix for GIFs
  • Loading branch information
mtgriego authored Aug 1, 2024
1 parent 5259705 commit 3e12d8d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kickstarter.ui.extensions

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
Expand All @@ -11,6 +12,7 @@ import com.bumptech.glide.integration.webp.decoder.WebpDrawable
import com.bumptech.glide.integration.webp.decoder.WebpDrawableTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
Expand Down Expand Up @@ -133,6 +135,8 @@ fun ImageView.loadWebp(url: String?, context: Context) {
val roundedCorners = RoundedCorners(1)
Glide.with(this)
.load(it)
.downsample(DownsampleStrategy.AT_LEAST)
.override(Resources.getSystem().displayMetrics.widthPixels, Resources.getSystem().displayMetrics.heightPixels)
.optionalTransform(roundedCorners)
.optionalTransform(WebpDrawable::class.java, WebpDrawableTransformation(roundedCorners))
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
Expand All @@ -144,13 +148,16 @@ fun ImageView.loadWebp(url: String?, context: Context) {
}
}
}

fun ImageView.loadGifImage(url: String?, context: Context) {
url?.let {
if (context.applicationContext.isKSApplication()) {
try {
Glide.with(context)
.asGif()
.load(it)
.downsample(DownsampleStrategy.AT_LEAST)
.override(Resources.getSystem().displayMetrics.widthPixels, Resources.getSystem().displayMetrics.heightPixels)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(this)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class ImageWithCaptionView @JvmOverloads constructor(
binding.imageView.visibility = VISIBLE
binding.composeViewImage.visibility = GONE
}

src.isGif() -> {
binding.imageView.visibility = VISIBLE
binding.imageView.loadGifImage(src, context)
binding.composeViewImage.visibility = GONE
}

else -> {
binding.composeViewImage.visibility = VISIBLE
binding.imageView.visibility = GONE
Expand Down

0 comments on commit 3e12d8d

Please sign in to comment.