4

I need to be able use FFImageLoading.ImageService to load a byte array I decoded from an image earlier, into a FFImageLoading.Views.ImageViewAsync object. The method ImageService.Instance.LoadImage(IImageLoaderTask task) seems to be the way but I have no idea how to set up an object of that interface and I can't find any references to using this type object on the source website.

How to load a byte[] into a ImageViewAsync object?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MJ33
  • 859
  • 12
  • 25

2 Answers2

3

In my case, it worked like this ...

Modal.cs

 public class User 
 {
   public byte[] Avatar { get; set; }
 }

ViewModel.cs

public ImageSource Avatar
{
    get  
    {
      return ImageSource.FromStream(()=> 
        {
          return new MemoryStream(this.User.Avatar);
        });
     }
}

View.xaml

<ffimageloading:CachedImage x:Name="userAvatar" 
 Source = "{Binding Avatar}"
    Grid.Column="0" Grid.Row="0"  >

</ffimageloading:CachedImage>
KaYLKann
  • 31
  • 3
2

Since you already have a byte[] you could you this with the LoadStream method.

Something like:

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

And this is the method to do the actual work.

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;
}

This should work.

pinedax
  • 9,246
  • 2
  • 23
  • 30