Xuan. UWP. Framework (2), xuan. uwp. framework

Source: Internet
Author: User

Xuan. UWP. Framework (2), xuan. uwp. framework

The previous chapter describes the basic usage of Xuan. UWP. Framework. ImageLib. This chapter describes how to use Xuan. UWP. Framework. ImageLib.

1. First, let's take a look at ImageLoader, one of the most important classes in Xuan. UWP. Framework. ImageLib.

You can see that ImageLoader adopts the singleton mode. The external methods are CaheImageAsync, GetImageStreamAsync, and GetStorageFileFromCache. The first method of CaheImageAsync is to provide external StorageFile cache for common management. GetImageStreamAsync provides a Stream for obtaining downloaded images. GetStorageFileFromCache can obtain cached images through URLs for image processing or sharing.

For example:

  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 > 0) {                    var bit = new BitmapImage();                    await bit.SetSourceAsync(stream);                    img.Source = bit;                }            }

In the source code, GetImageStreamAsync uses several key methods: GetStreamFromUriAsync and 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 > 0) {                    await _config.StorageCache.SaveAsync(url, randomStream).ConfigureAwait(false);                }            }            return randomStream;        }

Specifically, GetStreamFromUriAsync uses the scheme of uri to determine whether a local resource file will be read directly, and if it is a network image, GetStreamFromCacheOrNetAsync will be used for loading. If the local cache exists, the local cache will be read directly. If the local cache is not saved, the image will be downloaded and cached. The specific content of GetStreamFromCacheOrNetAsync will be introduced in the next chapter.

ImageLoader also includes the Dependency Property of the SourceProperty type DependencyProperty, which can be considered as an additional Property of the Image. Easy to use in 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"/>

 

 

2. Before using ImageLoader, You need to configure ImageLoaderConfiguration. Currently, ImageLoaderConfiguration eventually requires StorageCache. StorageCache can implement the abstract class StorageCacheBase by itself. You can also use the default StorageCache.

StorageCache needs to configure the cache directory, as well as the file name encryption and cache time.

    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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.