2

How can I load a bitmap into an ImageViewAsync on Xamarin Android Native?

Jim Layhey
  • 353
  • 5
  • 13
  • Does this answer your question? [How do load a image stored locally in byte array using FFImageLoading for Xamarin?](https://stackoverflow.com/questions/44120859/how-do-load-a-image-stored-locally-in-byte-array-using-ffimageloading-for-xamari) – haZya Apr 02 '20 at 14:34

1 Answers1

2

You can use LoadStream method, here you will see how to use this method:

ImageService.Instance
            .LoadStream (GetStreamFromImageByte)
            .Into (imageView);

Here is the GetStreamFromImageByte:

Task<Stream> GetStreamFromImageByte (CancellationToken ct)
{
    //Here you set your bytes[] (image)
    byte [] imageInBytes = null;

    //Since we need to return a Task<Stream> we will use a TaskCompletionSource>
    TaskCompletionSource<Stream> tcs = new TaskCompletionSource<Stream> ();

    tcs.TrySetResult (new MemoryStream (imageInBytes));

    return tcs.Task;
}

About imageInBytes, you can look here, convert the bitmap to byte[]:

MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
byte[] bitmapData = stream.ToArray();

I have posted my demo on github.

Robbit
  • 4,300
  • 1
  • 13
  • 29