首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >加载大量图像(OutOfMemory异常)。如何直接从硬盘加载图像?

加载大量图像(OutOfMemory异常)。如何直接从硬盘加载图像?
EN

Stack Overflow用户
提问于 2013-05-02 01:55:35
回答 1查看 234关注 0票数 0

我试图加载大量的图像作为GridApp模板的DataSource。我使用递归函数遍历文件夹:

代码语言:javascript
运行
复制
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);
        }
    }

下面是核心功能:

代码语言:javascript
运行
复制
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应用程序可以使用吗?有没有可能仍然是异步的?

我知道我的代码需要重构。我不是要求重写它。我只需要一个在这种情况下如何节省内存的建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-02 01:58:49

您不可能加载所有这些文件,而不会遇到与内存相关的问题。您需要加载文件位置,映射它们,并可能创建可在应用程序中显示的图片的缩略图版本。然后,一旦你真的需要加载图片,你就可以卸载之前的任何图片,并加载你真正需要的图片。

当您需要查找要在屏幕上加载的下一个文件时,只需使用应用程序中缓存的位置加载图片即可。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16323598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档