I have a scenario of loading bitmap to a ImageView from a byteArray. I use the following code to achieve this.
Inside a class that extends ImageView
this.Post(() =>
{
using (customImage = BitmapFactory.DecodeByteArray(byteArray, 0, byteArray.Count(), options))
{
using (var renderBitemap = customImage.Copy(Bitmap.Config.Argb4444, true))
{
customImage?.Recycle();
options.Dispose();
options = null;
if (m_pdfParent.undisposedPageImages.Contains(m_pageIndex))
{
this.SetImageBitmap(renderBitemap);
}
stream = null;
}
});
As you can see the bitmap conversion also occurs in the UI thread (It is a heavy process, right?), this blocks the UI.
1) On using only the SetimageBitmap method in the UI thread => I get an object disposed exception.
2) On removing the this.Post and running everything in the background thread => I get change an exception that only the thread that created a view can alter the View.
Is there a way to improve this code snippet?(Setting bitmap from byteArray to an ImageView without blocking the UI thread)