I've tried below code but when I scroll, it lag too much and it crashes after some time it tries to load images.
val productImage = imgStringBase64.convertBitMap()
Glide.with(context)
.asBitmap()
.apply(requestOptions)
.load(productImage)
.placeholder(R.drawable.ic_no_image)
.into(imgProduct)
here's my function,
private fun loadImage(
imgProduct: ImageView,
imgString: String
) {
CoroutineScope(Dispatchers.IO).launch {
val productImage = imgString.convertBitMap()
withContext(Dispatchers.Main) {
val requestOptions = RequestOptions()
.downsample(DownsampleStrategy.DEFAULT)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.override(300, 300)
Glide.with(context)
.asBitmap()
.apply(requestOptions)
.load(productImage)
.placeholder(R.drawable.ic_no_image)
.into(imgProduct)
}
}
}
thank you in advance.
CodePudding user response:
I suggest coil instead of glide. Its image loading speed is faster. Add diffUtil to your recyclerviews adapter. I am using the extension function below to convert base 64 to bitmap. I don't know what kind of lag you encountered in this case, but give it a try, maybe it will be faster
@BindingAdapter(value = ["app:imageStr"])
fun ImageView.bindImageStr(
imageStr: String?
) {
imageStr?.let {
try {
val imageBytes = Base64.decode(it, Base64.DEFAULT)
val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,
imageBytes.size)
setImageBitmap(decodedImage)
} catch (e: Exception) {
e.message
}
}
}
CodePudding user response:
It's lag because when you scroll then its converting Base64 to Bitmap every-time for each item.
I will suggest to save Base64 into file and save into local storage (if file not exist) and give local storage path to Glide.
So if file saved once into local storage then directly used it instead of again write file.
It will increase performance.