上一章主要介绍了Xuan.UWP.Framework.ImageLib的基本用法,这一章具体来看些Xuan.UWP.Framework.ImageLib的使用。

一、首先看下Xuan.UWP.Framework.ImageLib中最主要的class之一的ImageLoader。

可以看到ImageLoader采用单例模式,外部提供的方法为CaheImageAsync,GetImageStreamAsync和GetStorageFileFromCache三个方法。第一个方法CaheImageAsync是提供了外部StorageFile 缓存共同管理。GetImageStreamAsync提供了获取下载图片的Stream,GetStorageFileFromCache可以通过url来获得缓存的图片用于图片处理或者分享等等。

例如:

  using (var stream = await ImageLib.ImageLoader.GetInstance.GetImageStreamAsync(@"http://ecx.images-amazon.com/images/I/512Pd6birKL.jpg",
null, new System.Threading.CancellationToken())) {
if (stream != null && stream.Size > ) {
var bit = new BitmapImage();
await bit.SetSourceAsync(stream);
img.Source = bit;
}
}

源码中 GetImageStreamAsync 使用了几个关键的方法GetStreamFromUriAsync和GetStreamFromCacheOrNetAsync

 protected virtual async Task<IRandomAccessStream> GetStreamFromUriAsync(Uri uri, CancellationToken cancellationToken) {
switch (uri.Scheme) {
case "ms-appx":
case "ms-appdata": {
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
return await file.OpenAsync(FileAccessMode.Read).AsTask(cancellationToken).ConfigureAwait(false);
}
case "ms-resource": {
var rm = ResourceManager.Current;
var context = ResourceContext.GetForCurrentView();
var candidate = rm.MainResourceMap.GetValue(uri.LocalPath, context);
if (candidate != null && candidate.IsMatch) {
var file = await candidate.GetValueAsFileAsync();
return await file.OpenAsync(FileAccessMode.Read).AsTask(cancellationToken).ConfigureAwait(false);
}
throw new Exception("Resource not found");
}
default: {
return null;
}
}
}
 protected virtual async Task<IRandomAccessStream> GetStreamFromCacheOrNetAsync(string url, DisplayImageOptions options) {
IRandomAccessStream randomStream = null;
randomStream = await GetStreamFromCacheAsync(url).ConfigureAwait(false);
if (randomStream == null) {
randomStream = await GetStreamFromNetAsync(url).ConfigureAwait(false);
if (options.CacheOnStorage && randomStream != null && randomStream.Size > ) {
await _config.StorageCache.SaveAsync(url, randomStream).ConfigureAwait(false);
}
}
return randomStream;
}

其中GetStreamFromUriAsync很简单通过uri的scheme来判断如果是本地资源文件将直接读取,如果是网络图片将使用GetStreamFromCacheOrNetAsync进行加载。如果存在本地缓存,将直接读取本地缓存,如果本地不存再缓存则将下载图片并缓存图片。GetStreamFromCacheOrNetAsync中的具体内容将放到下一章来介绍。

ImageLoader还包含了SourceProperty类型为DependencyProperty依赖属性这里可以认为是Image的附加属性。方便再Xaml中使用。

xmlns:imageloader="using:Xuan.UWP.Framework.ImageLib"

    <DataTemplate x:Key="SimpleImageDataTemplate">
<Grid>
<Image imageloader:ImageLoader.Source="{Binding Url}" Width="200" Height="200"/>
</Grid>
</DataTemplate>
   <Image x:Name="img" imageloader:ImageLoader.Source="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"/>

二、ImageLoader再使用前需要配置ImageLoaderConfiguration 目前ImageLoaderConfiguration最终要的当属StorageCache,StorageCache可以自行实现抽象类StorageCacheBase。也可以使用默认实现StorageCache。

StorageCache 需要配置缓存的目录,以及文件名称的加密和缓存的时间。

    public App() {
this.InitializeComponent();
this.Suspending += OnSuspending;
var configuration = new ImageLib.Config.ImageLoaderConfiguration.Builder()
.StorageCache(new ImageLib.Cache.StorageCache(ApplicationData.Current.LocalCacheFolder,
"test", new ImageLib.Cache.SHA1CacheGenerator(), 0))
.Build();
ImageLib.ImageLoader.GetInstance.InitConfig(configuration);
}

Github : https://github.com/skyyuxuan/Xuan.UWP.Framework

Xuan.UWP.Framework(2)的更多相关文章

  1. Xuan.UWP.Framework

    开篇博客,以前总是懒,不喜欢写博客什么,其实都是给自己找理由,从今天开始有空就写写博客.新手博客,写得不好轻喷,哈哈! 开始正题,微软移动平台,从WP7开始,经历了WP8,然后WP8.1,到目前得Wi ...

  2. win10 uwp MVVM 轻量框架

    如果在开发过程,遇到多个页面之间,需要传输信息,那么可能遇到设计的问题.如果因为一个页面内包含多个子页面和多个子页面之间的通信问题找不到一个好的解决方法,那么请看本文.如果因为ViewModel代码越 ...

  3. 2019-11-29-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2019-11-29 10:16: ...

  4. 2018-9-1-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2018-09-01 16:24: ...

  5. 2018-10-22-win10-uwp-自定义控件入门

    title author date CreateTime categories win10 uwp 自定义控件入门 lindexi 2018-10-22 09:47:54 +0800 2018-10- ...

  6. 2018-8-10-win10-uwp-MVVM-轻量框架

    title author date CreateTime categories win10 uwp MVVM 轻量框架 lindexi 2018-08-10 19:17:19 +0800 2018-2 ...

  7. windows类书的学习心得

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  8. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

  9. [UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一)

    前言 本文中,您将创建一个通用应用程序(UWP),使用Entity Framework Core(Entity Framework 7)框架在SQLite数据库上执行基本的数据访问. 准备: Enti ...

随机推荐

  1. IE11,Chrome65,Firefox58 的webdriver驱动下载,调用浏览器打开网址

    一.环境及需求 1.1环境 Windows10 + Python 3.6.4 + selenium 3.141 1.2需求 工作需要实现一个网页自动登录的操作,决定使用selenium+python实 ...

  2. vue中调用地图

    一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 这个就不细说了,按照其文档,就能够安装下来. 二. 按照官方提供的方法引入 1.修改webpac.base.conf.js文件 ...

  3. 可决系数R^2和MSE,MAE,SMSE

    波士顿房价预测 首先这个问题非常好其实要完整的回答这个问题很有难度,我也没有找到一个完整叙述这个东西的资料,所以下面主要是结合我自己的理解和一些资料谈一下r^2,mean square error 和 ...

  4. Network Security Threats

    Network Security Combination of low-cost powerful computing and high-performance networks is a two-e ...

  5. ARP, Fragmentation and Reassembly

    Address Resolution Protocol IP addresses are said to be logical, because they are defined in terms o ...

  6. Subnet Routing Examples

    Routing Table Each row in routing table contains: Destination IP address IP address of next-hop rout ...

  7. 初窥UIKit Dynamics

    原文来自这里. iOS7中可以方便的给物体添加动态物理特性,主要使用到UIDynamicAnimator,UIDynamicBehavior以及实现了UIDynamicItem协议的对象.在iOS7中 ...

  8. C++切勿混用带符号类型和无符号类型

    如果表达式里既有带符号类型又有无符号类型,当带符号类型取值为负时会出现异常结果. 因为带符号数会自动转化为无符号数. 例如 a*b,a=-1, b=1,a是int,b是unsigned int,如果在 ...

  9. chromium之histogram.h

    histogram不知道是干啥的 // Histogram is an object that aggregates statistics, and can summarize them in // ...

  10. chromium之message_pump_win之一

    写了22篇博文,终于到这里了———— MessagePumpWin!!! MessagePumpWin这个类还是挺复杂的,可以分成好几部分.接下来分块分析 从介绍看,MessagePumpWin 是M ...