Skip to content

Commit

Permalink
use image thumbnail to limit memory increase
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 17, 2024
1 parent 2f11d34 commit a1f5138
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Starward/Controls/CachedImage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.WinUI.UI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Starward.Services.Cache;
Expand All @@ -7,6 +8,8 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.FileProperties;

namespace Starward.Controls;

Expand All @@ -23,14 +26,42 @@ public static void ClearCache()
}



public bool IsThumbnail
{
get { return (bool)GetValue(IsThumbnailProperty); }
set { SetValue(IsThumbnailProperty, value); }
}

public static readonly DependencyProperty IsThumbnailProperty =
DependencyProperty.Register("IsThumbnail", typeof(bool), typeof(CachedImage), new PropertyMetadata(false));




protected override async Task<ImageSource> ProvideCachedResourceAsync(Uri imageUri, CancellationToken token)
{
try
{
if (imageUri.Scheme is "ms-appx" or "file")
if (imageUri.Scheme is "ms-appx")
{
return new BitmapImage(imageUri);
}
else if (imageUri.Scheme is "file")
{
if (IsThumbnail)
{
StorageFile file = await StorageFile.GetFileFromPathAsync(imageUri.LocalPath);
StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, 240, ThumbnailOptions.UseCurrentScale);
BitmapImage image = new BitmapImage();
await image.SetSourceAsync(thumbnail);
return image;
}
else
{
return new BitmapImage(imageUri);
}
}
else
{
if (fileCache.TryGetValue(imageUri, out var uri))
Expand Down
2 changes: 2 additions & 0 deletions src/Starward/Pages/ImageViewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<DataTemplate x:DataType="sm:ScreenshotItem">
<sc:CachedImage Height="80"
EnableLazyLoading="True"
IsCacheEnabled="True"
IsThumbnail="True"
Source="{x:Bind FullName}" />
</DataTemplate>
</GridView.ItemTemplate>
Expand Down
2 changes: 2 additions & 0 deletions src/Starward/Pages/ScreenshotPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
ui:VisualExtensions.NormalizedCenterPoint="0.5"
Background="{ThemeResource ControlAltFillColorTertiaryBrush}"
EnableLazyLoading="True"
IsCacheEnabled="True"
IsThumbnail="True"
Source="{x:Bind FullName}"
Stretch="UniformToFill">
<animations:Explicit.Animations>
Expand Down

0 comments on commit a1f5138

Please sign in to comment.