How do load a image stored locally in byte array using FFImageLoading for Xamarin?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 04:09:26

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!