mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Unified ImageCacheItem
This commit is contained in:
parent
e138cb83ab
commit
2709f90cdc
12 changed files with 133 additions and 176 deletions
|
|
@ -1,22 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2021 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage.Streams;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
public class ImageCacheItem
|
||||
{
|
||||
public IBuffer Buffer { get; set; }
|
||||
public DateTime Expiration { get; set; }
|
||||
}
|
||||
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
Task SetAsync(string key, IBuffer buffer, DateTime expiration);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage.Streams;
|
||||
|
||||
namespace MapControl.Caching
|
||||
{
|
||||
|
|
@ -15,7 +13,7 @@ namespace MapControl.Caching
|
|||
{
|
||||
public async Task<ImageCacheItem> GetAsync(string key)
|
||||
{
|
||||
ImageCacheItem imageCacheItem = null;
|
||||
ImageCacheItem cacheItem = null;
|
||||
var path = GetPath(key);
|
||||
|
||||
try
|
||||
|
|
@ -25,9 +23,9 @@ namespace MapControl.Caching
|
|||
var buffer = await File.ReadAllBytesAsync(path);
|
||||
var expiration = ReadExpiration(ref buffer);
|
||||
|
||||
imageCacheItem = new ImageCacheItem
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer.AsBuffer(),
|
||||
Buffer = buffer,
|
||||
Expiration = expiration
|
||||
};
|
||||
|
||||
|
|
@ -39,14 +37,14 @@ namespace MapControl.Caching
|
|||
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
|
||||
}
|
||||
|
||||
return imageCacheItem;
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
public async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
||||
public async Task SetAsync(string key, ImageCacheItem cacheItem)
|
||||
{
|
||||
var path = GetPath(key);
|
||||
|
||||
if (buffer != null && buffer.Length > 0 && path != null)
|
||||
if (cacheItem.Buffer != null && cacheItem.Buffer.Length > 0 && path != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -54,8 +52,8 @@ namespace MapControl.Caching
|
|||
|
||||
using (var stream = File.Create(path))
|
||||
{
|
||||
await stream.AsOutputStream().WriteAsync(buffer);
|
||||
await WriteExpirationAsync(stream, expiration);
|
||||
await stream.WriteAsync(cacheItem.Buffer, 0, cacheItem.Buffer.Length);
|
||||
await WriteExpirationAsync(stream, cacheItem.Expiration);
|
||||
}
|
||||
|
||||
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, expiration.ToLocalTime());
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
|
|
@ -29,14 +28,16 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(IBuffer buffer)
|
||||
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(buffer);
|
||||
stream.Seek(0);
|
||||
return LoadImageAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
return await LoadImageAsync(stream);
|
||||
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||
{
|
||||
using (var stream = new MemoryStream(buffer))
|
||||
{
|
||||
return LoadImageAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,15 +57,5 @@ namespace MapControl
|
|||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||
{
|
||||
return LoadImageAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||
{
|
||||
return LoadImageAsync(buffer.AsBuffer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,7 +186,6 @@
|
|||
<Link>WorldMercatorProjection.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Animatable.UWP.cs" />
|
||||
<Compile Include="ImageCache.UWP.cs" />
|
||||
<Compile Include="ImageFileCache.UWP.cs" />
|
||||
<Compile Include="Map.UWP.cs" />
|
||||
<Compile Include="MapBase.UWP.cs" />
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using MapControl.Caching;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
|
@ -15,6 +15,16 @@ using Microsoft.UI.Xaml.Media;
|
|||
|
||||
namespace MapControl
|
||||
{
|
||||
namespace Caching
|
||||
{
|
||||
public interface IImageCache
|
||||
{
|
||||
Task<ImageCacheItem> GetAsync(string key);
|
||||
|
||||
Task SetAsync(string key, ImageCacheItem cacheItem);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class TileImageLoader
|
||||
{
|
||||
/// <summary>
|
||||
|
|
@ -29,7 +39,7 @@ namespace MapControl
|
|||
/// <summary>
|
||||
/// The IImageCache implementation used to cache tile images. The default is null.
|
||||
/// </summary>
|
||||
public static Caching.IImageCache Cache { get; set; }
|
||||
public static IImageCache Cache { get; set; }
|
||||
|
||||
|
||||
private static async Task LoadCachedTileAsync(Tile tile, Uri uri, string cacheKey)
|
||||
|
|
@ -43,9 +53,15 @@ namespace MapControl
|
|||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
buffer = response.Buffer?.AsBuffer(); // may be null or empty when no tile available, but still be cached
|
||||
buffer = response.Buffer; // may be null or empty when no tile available, but still be cached
|
||||
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
cacheItem = new ImageCacheItem
|
||||
{
|
||||
Buffer = buffer,
|
||||
Expiration = GetExpiration(response.MaxAge)
|
||||
};
|
||||
|
||||
await Cache.SetAsync(cacheKey, cacheItem).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue