Ffimageloading: How to implement "no cache" with ImageSource

Created on 1 Feb 2018  路  4Comments  路  Source: luberda-molinet/FFImageLoading

Description

I can not remove the cache. Can you help me?

Steps to Reproduce

I add the first image and it appears in the listing. When I add the second image, it brings in the cache of the first one. I would like to remove the cache so this does not happen anymore.

Basic Information

Reproduction Link / Code

//Model
public class ImageModel : FileModel
{
        public ImageSource Source { get; set; }
}

//ViewModel
public class ViewModel : BaseViewModel
{
    public ObservableRangeCollection<ImageModel> List { get; } = new ObservableRangeCollection<ImageModel >();

    public ICommand SelectPhotoCommand => new Command(async () => await SelectPhotoExecutedAsync());

    private async Task SelectPhotoExecutedAsync()
    {
        try
        {
            await CrossMedia.Current.Initialize();

            MediaFile file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions()
            {
                PhotoSize = PhotoSize.Medium,
                SaveMetaData = false
            });

            await ConvertMediaFileToImageModel(file);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

    private async Task ConvertMediaFileToImageModel(MediaFile file)
    {
        Stream stream = file.GetStream();

        string fileName = $"{Guid.NewGuid().ToString()}.jpg";

        ImageModel item = new ImageModel()
        {
            Content = ReadFully(stream),
            FileName = fileName,
            Source = ImageSource.FromStream(() => stream)
        };

        //NOT WORKING
        await CachedImage.InvalidateCache(item.Source, CacheType.All, true);

        List.Add(item);
    }
}

//XAML
<controls:CarouselViewControl 
    ItemsSource="{Binding List}" 
    ShowIndicators="True"
    VerticalOptions="FillAndExpand" 
    HorizontalOptions="FillAndExpand">
    <controls:CarouselViewControl.ItemTemplate>
        <DataTemplate>
            <ContentView>
                <AbsoluteLayout>
                    <ffimageloading:CachedImage 
                        x:Name="cachedImage" CacheDuration="-1"
                        Source="{Binding Source}" Aspect="AspectFill"
                        AbsoluteLayout.LayoutBounds="0.0, 0.0, 1.0, 1.0" AbsoluteLayout.LayoutFlags="All">
                    </ffimageloading:CachedImage>

                    <Button Text="X"
                            Command="{Binding Path=BindingContext.DeletePhotoCommand, Source={x:Reference ListPage}}"
                            CommandParameter="{Binding .}" 
                            AbsoluteLayout.LayoutFlags="PositionProportional" 
                            AbsoluteLayout.LayoutBounds="1,0,AutoSize,AutoSize"/>
                </AbsoluteLayout>
            </ContentView>
        </DataTemplate>
    </controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>

Most helpful comment

I Implemented CacheType.None, I'll release a stable nuget today. Cheers.

All 4 comments

hi @matheusvelloso

Try to set

=> CacheType="Memory"
=> CacheDuration="-1"

And call this, when imageSource changing
=> ImageService.Instance.InvalidateCacheAsync(Cache.CacheType.All);

I have the same issue on android, @AndreiMisiukevich suggestion worked for me but of course is not ideal.

hi @AndreiMisiukevich thanks

Its worked!

I Implemented CacheType.None, I'll release a stable nuget today. Cheers.

Was this page helpful?
0 / 5 - 0 ratings