Ppdac.Cache 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ppdac.Cache --version 1.0.0
NuGet\Install-Package Ppdac.Cache -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ppdac.Cache" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ppdac.Cache --version 1.0.0
#r "nuget: Ppdac.Cache, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ppdac.Cache as a Cake Addin
#addin nuget:?package=Ppdac.Cache&version=1.0.0

// Install Ppdac.Cache as a Cake Tool
#tool nuget:?package=Ppdac.Cache&version=1.0.0

ImageCache

This library allows one to save bandwidth or API calls by caching images locally in a cache folder.

For .NET MAUI apps, the location of this folder is decided by FileSystem.CacheDirectory, and for .NET 6.0 Windows apps, it is decided by Environment.SpecialFolder.ApplicationData.

In place of the URI, use GetAsImageSourceAsync(Uri uri) instead, which will remember if this URI has been cached before, and if so, return the cached image source instead of downloading it again.

Because it is derived from the general-purpose ImageCache class, it can also be used for other classes not specific to .NET MAUI, such as Bitmaps, or other image types that can accept byte arrays or streams.

For use with types that use an ImageSource, you can use the derived ImageCache class in Ppdac.Cache.Maui use GetAsImageSourceAsync(Uri uri) instead.

Usage

Let's say you have an Image control that you currently pass a URL string into the Source property, instead of passing the string do something like:

Source = await ImageCache.GetAsImageSourceAsync("https://www.example.com/image.png");
// or ideally:
Source = await ImageCache.GetAsImageSourceAsync(new Uri("https://www.example.com/image.png"));

// Or use the ImageSource type directly:
ImageSource imageSource = await ImageCache.GetAsImageSourceAsync(imageUri);
Microsoft.Maui.Controls.Image mmcImage = new Microsoft.Maui.Controls.Image
{
	Source = imageSource
};

It also works with controls that expect a byte array or stream, System.Drawing.Image, or the Bitmap class:

System.Drawing.Bitmap sdBitmap = new Bitmap(await _imageStore.GetAsStreamAsync(uri));

// If you prefer synchronous methods:
System.Drawing.Image sdImage = System.Drawing.Image.FromStream(_imageStore.GetAsStreamAsync(uri).Result);

// Or just set up your byte array, and you can use these anywhere!
byte[] imageBytes = await _imageStore.GetAsByteArrayAsync(uri);

As you can see, you are simply establishing a source for the image, but having a helper function sit right in the middle.

Advanced Usage

You may only want to use it on certain pages. There are several ways to do this, including with dependcy injection, as well as changing the default cache folder to whatever you like:

// Page A
_imageCache.ImageCachePath = nameof(MyPage);

// Page B
_imageCacheB.ImageCachePath = nameof(MyOtherPage);

Though, DI is probably the best way to do this, injecting the class into the desired page.

Contibuting

You are actively encouraged to report bugs and contribute to this repository.

Contributions Are Appreciated and Welcome

  • If you want to improve this library please make a pull request.

Bugs and Issues

License Awareness

You should be aware of the license of all required or optional Nuget dependencies including .NET libraries published on nuget.org or elsewhere including: * Microsoft.Maui.Controls under MIT licensing (like this is). * Microsoft.Maui.Storage under MIT licensing.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-android33.0 is compatible.  net7.0-ios was computed.  net7.0-ios16.1 is compatible.  net7.0-maccatalyst was computed.  net7.0-maccatalyst16.1 is compatible.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net7.0-windows8.0 is compatible.  net7.0-windows10.0.17763 is compatible.  net7.0-windows10.0.19041 is compatible.  net7.0-windows10.0.22000 is compatible.  net7.0-windows10.0.22621 is compatible.  net8.0 is compatible.  net8.0-android was computed.  net8.0-android34.0 is compatible.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-ios17.0 is compatible.  net8.0-maccatalyst was computed.  net8.0-maccatalyst17.0 is compatible.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net8.0-windows8.0 is compatible.  net8.0-windows10.0.17763 is compatible.  net8.0-windows10.0.19041 is compatible.  net8.0-windows10.0.22000 is compatible.  net8.0-windows10.0.22621 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ppdac.Cache:

Package Downloads
Ppdac.Cache.Maui

Use this middleware to cache images from the Internet. Wherever you would give a control a URL, a stream, or byte[], continue doing that as normal, but have ImageCache act as the intermediary. For example, instead of ImageSource = Uri, do ImageSource = ImageCache(Uri).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 215 11/30/2023
1.0.0 99 11/30/2023

This version no longer uses static methods and .NET MAUI-specific content now in its own project: Ppdac.Cache.Maui.