Skip to content

Instantly share code, notes, and snippets.

@TemMax
Last active January 13, 2021 21:35
Show Gist options
  • Save TemMax/f768a97e00f380929a104c9da41413ee to your computer and use it in GitHub Desktop.
Save TemMax/f768a97e00f380929a104c9da41413ee to your computer and use it in GitHub Desktop.
Postprocessor for Facebook's Fresco library that combines few postprocessors
import android.graphics.Bitmap
import com.facebook.common.references.CloseableReference
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory
import com.facebook.imagepipeline.request.BasePostprocessor
class CombinePostProcessors(
private val processors: List<BasePostprocessor>
) : BasePostprocessor() {
override fun process(
sourceBitmap: Bitmap,
bitmapFactory: PlatformBitmapFactory
): CloseableReference<Bitmap> {
var result: CloseableReference<Bitmap>? = null
for (processor in processors) {
val src = result?.get() ?: sourceBitmap
result = processor.process(src, bitmapFactory)
}
return result ?: with(bitmapFactory.createBitmap(sourceBitmap)) {
try {
CloseableReference.cloneOrNull(this)!!
} finally {
CloseableReference.closeSafely(this)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment