How do load a image stored locally in byte array u

2019-06-06 01:02发布

问题:

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?

回答1:

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.



回答2:

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>