Search code examples
c#xamarinxamarin.androidffimageloading

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


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?


Solution

  • 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.