mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-04 14:08:32 +00:00
Version 4.12.2 Improved ImageLoader/TileImageLoader
This commit is contained in:
parent
4a7e384152
commit
fefc4e3294
9 changed files with 102 additions and 171 deletions
|
|
@ -15,11 +15,6 @@ namespace MapControl
|
|||
{
|
||||
public static partial class ImageLoader
|
||||
{
|
||||
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
||||
{
|
||||
return LoadImageAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
|
||||
{
|
||||
var image = new BitmapImage();
|
||||
|
|
@ -29,11 +24,11 @@ namespace MapControl
|
|||
return image;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
|
||||
public static async Task<ImageSource> LoadImageAsync(IBuffer buffer)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(buffer.AsBuffer());
|
||||
await stream.WriteAsync(buffer);
|
||||
stream.Seek(0);
|
||||
|
||||
return await LoadImageAsync(stream);
|
||||
|
|
@ -56,5 +51,15 @@ 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System;
|
|||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.UI.Core;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
||||
|
|
@ -32,37 +31,26 @@ namespace MapControl
|
|||
private static async Task LoadCachedTileImageAsync(Tile tile, Uri uri, string cacheKey)
|
||||
{
|
||||
var cacheItem = await Cache.GetAsync(cacheKey).ConfigureAwait(false);
|
||||
var cacheBuffer = cacheItem?.Buffer;
|
||||
var buffer = cacheItem?.Buffer;
|
||||
|
||||
if (cacheBuffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
if (buffer == null || cacheItem.Expiration < DateTime.UtcNow)
|
||||
{
|
||||
var response = await ImageLoader.GetHttpResponseAsync(uri, false).ConfigureAwait(false);
|
||||
|
||||
if (response != null) // download succeeded
|
||||
{
|
||||
cacheBuffer = null; // discard cached image
|
||||
buffer = response.Buffer.AsBuffer();
|
||||
|
||||
if (response.Stream != null) // tile image available
|
||||
if (buffer != null) // tile image available
|
||||
{
|
||||
using (var stream = response.Stream)
|
||||
{
|
||||
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(stream)).ConfigureAwait(false);
|
||||
|
||||
await Cache.SetAsync(cacheKey, stream.ToArray().AsBuffer(), GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
await Cache.SetAsync(cacheKey, buffer, GetExpiration(response.MaxAge)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cacheBuffer != null) // cached image not expired or download failed
|
||||
if (buffer != null)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(cacheBuffer);
|
||||
stream.Seek(0);
|
||||
|
||||
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(stream)).ConfigureAwait(false);
|
||||
}
|
||||
await SetTileImageAsync(tile, () => ImageLoader.LoadImageAsync(buffer)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue