Ayudh
Ayudh

Reputation: 1763

XamarinForms FFImageLoading CachedImage issue

I'm getting a .PNG image file from an API like so

public static async Task<CachedImage> GetImage(string UserID)
{
    var URL = "assumeThisUrlPointsToServer"

    HttpClient client = new HttpClient();

    Stream stream = await client.GetStreamAsync(URL);

    return new CachedImage { Source = ImageSource.FromStream(() => stream) };
}

and I am getting a file back and I am displaying it like so

<ffimageloading:CachedImage HeightRequest="52" Margin="13,0,16,0" Source="{Binding SourceOfReturnedCachedImage}"/>

Unfortunately, this is not working (blank). How do I get it to work?

Additional details : if I change it to an Image instead of CachedImage, then it works. Now the funny thing is that if i specify a URI instead of downloading a file, like so,

return new CachedImage { Source = ImageSource.FromUri('http://www.website.com/image.png')};

Then it (CachedImage) works!

Upvotes: 0

Views: 3837

Answers (2)

Daniel Luberda
Daniel Luberda

Reputation: 7454

Stream is disposed after every image load, you must modify your code to:

public static async Task<CachedImage> GetImage(string UserID)
{
    var URL = "assumeThisUrlPointsToServer"

    HttpClient client = new HttpClient();

    return new CachedImage { Source = ImageSource.FromStream(() => {
        return await client.GetStreamAsync(URL);
    })};
}

Upvotes: 2

Ayudh
Ayudh

Reputation: 1763

So xamarin provides a simple way to view images, you can just give the url of the image as the source of image/cachedimage so

<ffimageloading:CachedImage HeightRequest="52" Margin="13,0,16,0" Source="{Binding ImageURL}"/>

Where ImageURL is the URL I was downloading the image from

Upvotes: 0

Related Questions