Search code examples
xamarinxamarin.androidandroid-bitmapffimageloading

FFImageLoading load bitmap into ImageViewAsync


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


Solution

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