【发布时间】:2015-11-05 14:31:26
【问题描述】:
我正在使用 MvvmCross 在 Xamarin 中开发 Windows Phone 应用程序。在这个应用程序中,用户从他的手机中选择一些图像。它们显示在列表中,然后用户使用它们进行操作。
我使用 FileOpenPicker 进行文件选择,并从这些文件中创建 BitmapImages 来显示
foreach (StorageFile file in args.Files) {
BitmapImage thumbnail = new BitmapImage();
thumbnail.DecodePixelType = DecodePixelType.Physical;
try {
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) {
thumbnail.DecodePixelHeight = 70;
thumbnail.DecodePixelWidth = 70;
thumbnail.SetSource(fileStream);
fileStream.Dispose();
}
}
catch (OutOfMemoryException e) {
Mvx.Trace("MEMORY IS FULL");
}
在一些其他代码之后,我将这些 BitmapImages 放入 ObservableCollection 并像这样显示它们
<Image Style="{StaticResource imageListImage}" Source="{Binding Thumbnail}"/>
这没什么特别的。 我使用的测试图像的总大小为 34 MB。 使用 VS 的性能和诊断工具,我能够确定应用程序在启动时的内存使用量约为 16 Mb。当我将测试图像加载到应用程序时,它达到了 58 MB。好像它仍然使用图像的完整尺寸。并且(仅用于测试)当我将 decodepixelheight 和 width 移开时,它飙升至大约 350 MB。我完全不知道为什么它要为图像使用这么多内存。
因为应用程序必须能够使用更多更大的图像,所以我需要找到一种方法来减少内存使用量。有谁知道我该怎么做?
【问题讨论】:
标签: c# image xaml memory windows-phone-8