dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.01k stars 1.73k forks source link

Add clear image caching APIs #4766

Open rachelkang opened 2 years ago

rachelkang commented 2 years ago

Description

Currently, there are no APIs developers can leverage to clear the image cache and/or force an image to be retrieved from the original source (not from cache).

This should be further investigated, and if apt, the relevant APIs should be introduced. Whether this should be limited to images with URI sources should also be considered.

Public API Changes

public void ClearImageCache()
{
    foreach (string imagePath in Directory.GetFiles(CacheDirectory))
        File.Delete(imagePath);
}

Intended Use-Case

Give developers the capability to clear cache and/or force an image to be directly retrieved from the source more easily

Eilon commented 2 years ago

Adding a Clear() API could be nice but I think it isn't practical because it's a too big of a hammer. I think it's unlikely someone would want to clear all cached images.

For example, say the app has the user's profile image loaded. It's perfectly reasonable to cache it. But it's also perfectly reasonable to want to un-cache just that image so that an updated profile image could be loaded. You wouldn't want to clear out potentially thousands of cached items just to refresh one item.

I think to ship this with caching enabled by default, we must make some reasonable change to enable granular control of caching.

At this point I suggest either:

  1. Turn off caching by default, and make sure that the docs for the feature are clear that it cannot be cleared at all (at least, not yet)
  2. Leave it on by default, but we consider the pattern of how it's controlled (force reload, clear all cache, etc.)

If (2) is too expensive (we don't have enough time, etc.), then I strongly urge doing (1) because I fear that the current behavior could be detrimental to a lot of apps. Everyone who finds an issue with caching will just have to disable it in lots of places, assuming they even understand why it's happening.

Mielesgames commented 1 year ago

I am surprised this still hasn't been fixed, it has almost been a year since this issue got opened...

Eilon commented 1 year ago

I am surprised this still hasn't been fixed, it has almost been a year since this issue got opened...

Due to lack of demand this issue has had a low priority. Can you share more info about the pattern you'd like to see for this? In your app do you want to clear all the image cache, or just one particular image? Or some other pattern? Knowing more about what users want will help us determine the best way to approach this issue.

Mielesgames commented 1 year ago

I am surprised this still hasn't been fixed, it has almost been a year since this issue got opened...

Due to lack of demand this issue has had a low priority. Can you share more info about the pattern you'd like to see for this? In your app do you want to clear all the image cache, or just one particular image? Or some other pattern? Knowing more about what users want will help us determine the best way to approach this issue.

I am simply trying to not save an image in the cache, setting imagecaching to false does nothing, but I figured out I can set the CacheValidity to one minute does work unless you restart the app in less then a minute

Edit: Nvm, when you switch back and forth between a page that loads an image and another one a few times the app will stop loading and softlock

espenrl commented 1 year ago

I'm creating this app with the possibility of rotating a picture. The picture is saved locally on the phone and rotated programatically, then saved to the same file. At this point caching on Android becomes an issue as it shows the old picture. I will investigate it an create a separate issue when I have some time.

blmiles commented 11 months ago

Interesting that this is still an issue. Maui, Android 13, SDK 33, .Net 7.0

I have a graphics app that creates and makes changes to drawings and saves as bitmaps. The path to the bitmap is always saved on the drawing record in the local db. When a list loads the drawings, it binds the bitmap path to an image control. It only ever displays the original bitmap, never shows the changed/updated bitmap and yet when I export the same bitmap from the same path, I do get the amended image. But the list and image control won't let go of the cached file.

One would expect this to work:

 if (File.Exists(drawing.ImagePath) == true) File.Delete(drawing.ImagePath);
//Then save using same path and file name once again:
                using (var filestream = File.OpenWrite(drawing.ImagePath))
                {
                    data.SaveTo(filestream);
                    filestream.Close();
                }

This actually does save the changes. But the list and bound image control still sees the original (assume deleted but cached) file. This was never an issue with Xamarin Android apps. WHY has MS introduced this restriction? Sorry, no justification for it considering so many Xamarin apps are being ported over to Maui and THIS breaks things. Annoying and should not be the case.

blmiles commented 11 months ago

What a useful library for Maui Apps and this Image caching debacle: https://github.com/Redth/FFImageLoading.Compat Hope that helps a few here!!

Install the nuget package the add this to the MauiProgram.cs:

.UseFFImageLoading() Add this to the xaml page where you might have you Image control(s):

xmlns:ffimageloading="clr-namespace:FFImageLoading.Maui;assembly=FFImageLoading.Compat.Maui"

Substitute the Image xaml for this:

          <ffimageloading:CachedImage CacheType="None" Source="{Binding ImagePath}" Aspect="AspectFit">
              <ffimageloading:CachedImage.HeightRequest>
                  <OnIdiom x:TypeArguments="x:Double" Phone="200" Tablet="375"  />
              </ffimageloading:CachedImage.HeightRequest>                                    
          </ffimageloading:CachedImage>

Amend to suit you reqs...

mattleibow commented 10 months ago

Duplicate of https://github.com/dotnet/maui/issues/9773

mattleibow commented 10 months ago

Marking as a duplicate for linking reasons, but not a 100% duplicate. Configuration is closely related to clearing/deleting the cache.