我试图加载大量的图像作为GridApp模板的DataSource。我使用递归函数遍历文件夹:
public async void walk(StorageFolder folder)
{
IReadOnlyList<StorageFolder> subDirs = null;
subDirs = await folder.GetFoldersAsync();
foreach (var subDir in subDirs)
{
await SampleDataSource.AddGroupForFolderAsync(subDir.Path);
walk(subDir);
}
}
下面是核心功能:
public static async Task<bool> AddGroupForFolderAsync(string folderPath)
{
if (SampleDataSource.GetGroup(folderPath) != null) return false;
StorageFolder fd = await StorageFolder.GetFolderFromPathAsync(folderPath);
IReadOnlyList<StorageFile> fList1 = await fd.GetFilesAsync();
List<StorageFile> fList2 = new List<StorageFile>();
foreach (var file in fList1)
{
string ext = Path.GetExtension(file.Path).ToLower();
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg") fList2.Add(file);
}
if (fList2.Count != 0)
{
var folderGroup = new SampleDataGroup(
uniqueId: folderPath,
title: fd.Path,
subtitle: null,
imagePath: null,
description: "Description goes here");
foreach (var i in fList2)
{
StorageFile fl = await StorageFile.GetFileFromPathAsync(i.Path);
IRandomAccessStream Stream = await fl.OpenAsync(FileAccessMode.Read);
BitmapImage pict = new BitmapImage();
pict.SetSource(Stream);
if (pict != null && folderGroup.Image == null)
{
folderGroup.SetImage(pict);
}
var dataItem = new SampleDataItem(
uniqueId: i.Path,
title: i.Path,
subtitle: null,
imagePath: pict,
description: "Decription goes here",
content: "Content goes here",
@group: folderGroup);
folderGroup.Items.Add(dataItem);
}
AllGroups.Add(folderGroup);
return true;
}
else { return false; }
}
我需要加载大量的文件( 164个文件夹和超过1300个文件,总共500MB)。稍后,这个数量可能会更大。看起来像是IRandomAccessStream将文件加载到内存中。如何直接从硬盘加载图片到app?Windows-Store应用程序可以使用吗?有没有可能仍然是异步的?
我知道我的代码需要重构。我不是要求重写它。我只需要一个在这种情况下如何节省内存的建议。
发布于 2013-05-02 01:58:49
您不可能加载所有这些文件,而不会遇到与内存相关的问题。您需要加载文件位置,映射它们,并可能创建可在应用程序中显示的图片的缩略图版本。然后,一旦你真的需要加载图片,你就可以卸载之前的任何图片,并加载你真正需要的图片。
当您需要查找要在屏幕上加载的下一个文件时,只需使用应用程序中缓存的位置加载图片即可。
https://stackoverflow.com/questions/16323598
复制相似问题