Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ImageSource.FromStream with caching

$
0
0

My webservice return a custom JSON (with base64-encoded image byte data) instead of a normal byte content of image type.

Since just ImageSource.FromUrl has caching, I tried to implement it myself (using ImageDownloader from https://github.com/xamarin/prebuilt-apps/blob/5691957f41af865704eaae153f5fa9b88998a88c/EmployeeDirectory/EmployeeDirectory/Utilities/ImageDownloader.cs as basis)

I have a Command responsible to load my Image from the web or from the filesystem (when cached):

private ImageSource _image = null;
public ImageSource MainImageSource
{
    get { return _image; }
    set
    {
        _image = value;
        OnPropertyChanged("MainImageSource");
    }
}
// more code
public Command LoadImage
{
    get
    {
        return new Command(async (id) =>
        {
            var stream = await _imageDownloader.GetDraftImageAsync();
            MainImageSource = ImageSource.FromStream(() => stream);
        });
    }
}

The ImageDownloader uses the IsolatedStorage to save the image to the filesystem on the different platforms.

This is perfectly working, until I navigate away and come back to the view (Page)... When I navigate away, my streams (I've more since its a listview) are being disposed by Xamarin, when I come back, it tries to read from a disposed Stream and raises an ObjectDisposedException.

The question is, how can I avoid this? I was thinking like, if the stream is closed it will call again the LoadImage command, but how can I tell from the _image (ImageSource) field if has being disposed?

Or why can't just give the cache file path as ImageSource? If I use ImageSource.FromFile("pathToIsolatedStorageFile") is not working because FromFile expect a file which is Resource bundled to the application instead of being dynamically created.

Thanks a lot for any inputs


Viewing all articles
Browse latest Browse all 58056

Trending Articles