UWP关于图片缓存的那些破事儿
看似简单的功能,实施起来却是有着一堆大坑。
按着基本功能来写吧
1.选择图片并显示到Image控件中
2.图片序列化为byte数组以及反序列化
3.本地存储与读取
1.选择图片:
逻辑就是使用FileOpenPicker选择一个图片文件后,将该文件从StorageFile对象转化成可以赋值给Image.Source的ImageSource类的对象。
(这里需要注意的是WriteableBitmap,BitmapImage,SoftwareBitmapSource都可以赋值给Image.Source,你赋值给Image.Source的是什么对象,你就只能用as符把Image.Source转化成什么对象。 比如:你使用WriteableBitmap赋值给Image.Source,那么你后面只能用 var t = imageSource as WriteableBitmap, 你如果使用var t = imageSource as BitmapImage的话编译器不会报错,但是t的值最后会是null)
1.1 选取图片文件并实例化为WriteableBitmap对象
- private async void ChoosePhoto()
- {
- FileOpenPicker fileOpenPicker = new FileOpenPicker();
- fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- fileOpenPicker.FileTypeFilter.Add(".jpg");
- fileOpenPicker.FileTypeFilter.Add(".png");
- fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
- StorageFile imgFile_tmp = await fileOpenPicker.PickSingleFileAsync();
- if (imgFile_tmp == null)
- {
- return;
- }
- else
- {
- this.imgFile = imgFile_tmp;
- using (IRandomAccessStream stream = await imgFile.OpenAsync(FileAccessMode.Read))
- {
- var srcImage = new WriteableBitmap(,);
- await srcImage.SetSourceAsync(stream);
- Image_Thumbnail.Source = srcImage;
- }
- }
- }
1.2 选取图片文件并实例化为BitmapImage对象
- private async void ChoosePhoto()
- {
- FileOpenPicker fileOpenPicker = new FileOpenPicker();
- fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- fileOpenPicker.FileTypeFilter.Add(".jpg");
- fileOpenPicker.FileTypeFilter.Add(".png");
- fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
- StorageFile imgFile_tmp = await fileOpenPicker.PickSingleFileAsync();
- if (imgFile_tmp == null)
- {
- return;
- }
- else
- {
- this.imgFile = imgFile_tmp;
- using (IRandomAccessStream stream = await imgFile.OpenAsync(FileAccessMode.Read))
- {
- var srcImage = new BitmapImage();
- await srcImage.SetSourceAsync(stream);
- Image_Thumbnail.Source = srcImage;
- }
- }
- }
1.3 选取图片实例化为SoftwareBitmapSource对象
- private async void ChoosePhoto()
- {
- FileOpenPicker fileOpenPicker = new FileOpenPicker();
- fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
- fileOpenPicker.FileTypeFilter.Add(".jpg");
- fileOpenPicker.FileTypeFilter.Add(".png");
- fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
- StorageFile imgFile_tmp = await fileOpenPicker.PickSingleFileAsync();
- if (imgFile_tmp == null)
- {
- return;
- }
- else
- {
- SoftwareBitmap softwareBitmap;
- this.imgFile = imgFile_tmp;
- using (IRandomAccessStream stream = await imgFile.OpenAsync(FileAccessMode.Read))
- {
- // Create the decoder from the stream
- BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
- // Get the SoftwareBitmap representation of the file
- softwareBitmap = await decoder.GetSoftwareBitmapAsync();
- }
- if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
- {
- softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
- }
- var source = new SoftwareBitmapSource();
- await source.SetBitmapAsync(softwareBitmap);
- // Set the source of the Image control
- Image_Thumbnail.Source = source;
- }
- }
2.图片序列化为byte数组以及反序列化
2.1 WriteableBitmap对象(Image.Source赋值的时候使用的是WriteableBitmap)序列化为byte数组
- public static async Task<byte[]> SaveToBytesAsync(ImageSource imageSource)
- {
- byte[] imageBuffer;
- var localFolder = ApplicationData.Current.TemporaryFolder;
- var file = await localFolder.CreateFileAsync("temp.jpg", CreationCollisionOption.GenerateUniqueName);
- using (var ras = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
- {
- WriteableBitmap bitmap = imageSource as WriteableBitmap;
- var stream = bitmap.PixelBuffer.AsStream();
- byte[] buffer = new byte[stream.Length];
- await stream.ReadAsync(buffer, , buffer.Length);
- BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ras);
- encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, buffer);
- await encoder.FlushAsync();
- var imageStream = ras.AsStream();
- imageStream.Seek(, SeekOrigin.Begin);
- imageBuffer = new byte[imageStream.Length];
- var re = await imageStream.ReadAsync(imageBuffer, , imageBuffer.Length);
- }
- await file.DeleteAsync(StorageDeleteOption.Default);
- return imageBuffer;
- }
2.2Byte数组反序列化为ImageSource(WriteableBitmap)
- public static async Task<ImageSource> SaveToImageSource(byte[] imageBuffer)
- {
- ImageSource imageSource = null;
- try
- {
- using (MemoryStream stream = new MemoryStream(imageBuffer))
- {
- var ras = stream.AsRandomAccessStream();
- BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, ras);
- var provider = await decoder.GetPixelDataAsync();
- byte[] buffer = provider.DetachPixelData();
- WriteableBitmap bitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
- await bitmap.PixelBuffer.AsStream().WriteAsync(buffer, , buffer.Length);
- imageSource = bitmap;
- }
- }
- catch (Exception ex)
- {
- }
- return imageSource;
- }
2.3 BitmapImage对象序列化为byte数组
需要注意的是这里所调用的BitmspImage对象,必须是使用BitmapImage(uri)方法实例化出来的BitmapImage对象,因此下列方法主要用于实现对网络图片的序列化与反序列化。
如果使用从FileOpenPicker选取出来图片文件实例化出来的BitmapImage对象的话,该对象的UriSource属性为空,故下列方法无法实现。
- RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(himage.UriSource);
- IRandomAccessStreamWithContentType streamWithContent = await random.OpenReadAsync();
- byte[] buffer = new byte[streamWithContent.Size];
- await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
2.4 byte数组反序列化为BitmapImage对象
- BitmapImage image = new BitmapImage();
- using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
- {
- await stream.WriteAsync(obj.ImgArray.AsBuffer());
- stream.Seek();
- await image.SetSourceAsync(stream);
- }
3.本地存储与读取
(需要注意的是:StorageFile这类的存储操作貌似只允许存一些简单的数据类型,所以我的解决方法就是把自定义的类的对象里的图片转化为byte数组,然后再将自定义类的对象的List转化成json字符串存进文件,读取文件的时候再读出这些Json字符串并反序列化为我需要的类型)
3.1 存储数据
- public static async Task SaveData(string filename, T data)
- {
- try
- {
- StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
- using (IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite))
- {
- using (IOutputStream outStream = raStream.GetOutputStreamAt())
- {
- var json = JsonConvert.SerializeObject(data);
- DataContractSerializer serializer = new DataContractSerializer(typeof(string));
- serializer.WriteObject(outStream.AsStreamForWrite(), json);
- var t =JsonConvert.DeserializeObject<T>(json);
- await outStream.FlushAsync();
- }
- }
- }
- catch (Exception exc)
- {
- throw exc;
- }
- }
3.2 加载数据
- public static async System.Threading.Tasks.Task<List<ShowInfo>> LoadData(string filename)
- {
- try
- {
- if (ApplicationData.Current.LocalFolder.TryGetItemAsync(filename) != null)
- {
- StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
- using (IInputStream inStream = await file.OpenSequentialReadAsync())
- {
- DataContractSerializer serializer = new DataContractSerializer(typeof(string));
- var json = (string)serializer.ReadObject(inStream.AsStreamForRead());
- var data = JsonConvert.DeserializeObject<List<ShowInfo>>(json);
- return data;
- }
- }
- else
- {
- return new List<ShowInfo>();
- }
- }
- catch (FileNotFoundException ex)
- {
- throw ex;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
UWP关于图片缓存的那些破事儿的更多相关文章
- 关于CAE的那点儿破事儿
CAE是计算机辅助工程的英文简写,所涵盖的范围甚是广泛.现在很多人提到CAE,总是联想到结构有限元计算,更有甚者认为有限元就是CAE.还有人把所有的工程数值计算都称作有限元.本文就这一话题,来谈谈关于 ...
- 优化 UWP 中图片的内存占用
跟图片打交道的 UWP 应用或多或少都会遇到图片带来的性能问题,就算不主要处理图片,做个论坛做个新闻客户端都涉及到大量图片.一个帖子.一篇文章里多半都是些高清大图,这些图片一张即可占用程序 1~2M ...
- Android图片缓存之Lru算法
前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...
- android使用ImageLoader实现图片缓存(安卓开发必备)
相信大家在学习以及实际开发中基本都会与网络数据打交道,而这其中一个非常影响用户体验的就是图片的缓存了,若是没有弄好图片缓存,用户体验会大大下降,总会出现卡顿情况,而这个问题尤其容易出现在ListVie ...
- Android图片缓存之Glide进阶
前言: 前面学习了Glide的简单使用(Android图片缓存之初识Glide),今天来学习一下Glide稍微复杂一点的使用. 图片缓存相关博客地址: Android图片缓存之Bitmap详解 And ...
- Android图片缓存之初识Glide
前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...
- Android图片缓存之Bitmap详解
前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...
- AFNetworking图片缓存问题
AFNetworking网络库已经提供了很好的图片缓存机制,效率是比较高的,但是我发现没有直接提供清除缓存的功能,可项目通常都需要添加 清除功能的功能,因此,在这里我以UIImageView+AFNe ...
- IOS编程 图片缓存模块设计
手机客户端为什么会留存下来?而不是被一味的Wap替代掉?因为手机客户端有Wap无可替代的优势,就是自身较强的计算能力. 手机中不可避免的一环:图片缓存,在软件的整个运行过程中显得尤为重要. 先简单说一 ...
随机推荐
- icons 在线网站
icons https://www.iconfinder.com/ http://v3.bootcss.com/components/ http://fontawesome.io/icons/ htt ...
- 阿里巴巴 Weex
原文链接:https://blog.csdn.net/zz901214/article/details/79168707/ 分享嘉宾:侑夕 阿里巴巴高级前端工程师(上张帅哥的图镇楼,看完,更有动力学习 ...
- DNS 原理
一.DNS 是什么? DNS (Domain Name System 的缩写)的作用非常简单,就是根据域名查出IP地址.你可以把它想象成一本巨大的电话本. 举例来说,如果你要访问域名math.stac ...
- css fixed 失效问题解法
https://stackoverflow.com/questions/11258877/fixed-element-disappears-in-chrome 开启css硬件加速,transform: ...
- BIF
list()把一个可迭代对象转化为列表 tuple()把一个可迭代对象转化为元祖 str()把参数对象转化为字符串 len()返回参数的长度 max()返回序列或者参数集合中的最大值 min()返回序 ...
- How to change system keyboard keymap layout on CentOS 7 Linux
The easiest way to swap between keymaps and thus temporarily set keys to different language by use o ...
- select2插件用法
1.修改默认查询方法,使其可以根据value查询 this.element.select2({ allowClear: true, matcher: function (term, text, ele ...
- MySQL解压包的安装教程
一.下载MySQL解压包 解压过的文件夹里面是没有 data 文件夹的. 二.创建文件 1.在根目录下创建 my.ini文件 内容如下: [mysqld] # 设置mysql的安装目录 basedir ...
- 在html中使用javascript总结
对于初学者运行代码的第一步,首先是怎么把你所写的js代码与html代码之间关联起来,只有关联了,js才能控制html中的代码,进而达到控制页面的目的,我总结了html引用js的方法,一方面可以时时复习 ...
- 【机器学习】异常检测算法(I)
在给定的数据集,我们假设数据是正常的 ,现在需要知道新给的数据Xtest中不属于该组数据的几率p(X). 异常检测主要用来识别欺骗,例如通过之前的数据来识别新一次的数据是否存在异常,比如根据一个用户以 ...