【发布时间】:2015-01-29 23:11:30
【问题描述】:
我正在构建一个 Windows Phone 8.1 应用程序(Windows 运行时,而不是 Windows Phone Silverlight 8.1)。在我的应用程序中,我需要在 GridView 中显示 CameraRoll 的所有照片,但显示为缩略图,以减少内存使用量。当我尝试我的应用程序时,一切正常,但速度非常慢。
我的代码如下:
===================== MainPage.xaml.cs=============================
var files = await KnownFolders.CameraRoll.GetFilesAsync();
List<ImageSource> imageSources = new List<ImageSource>();
for(int i=0; i<files.Count; i++)
{
await ExecuteCode(i, files, KnownFolders.CameraRoll, imageSources);
}
photosGrid.DataContext = imageSources;
private async Task ExecuteCode(int index, IReadOnlyList<StorageFile> files, StorageFolder folder, List<ImageSource> imageSources)
{
uint requestedSize = 90;
using(StorageItemThumbnail itemThumbnail = await files[index].GetThumbnailAsync(ThumbnailMode.PicturesView, requestedSize))
{
using(IRandomAccessStream fStream = itemThumbnail.AsStreamForRead().AsRandomAccessStream())
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fStream);
imageSources.Add(bitmapImage);
bitmapImage = null;
GC.AddMemoryPressure((long)itemThumbnail.Size);
}
}
}
========================MainPage.xaml==========================
<GridView x:Name="photosGrid" Height="392" Width="400" ItemsSource="{Binding}" Margin="0,0,-0.333,0" SelectionMode="Multiple" Background="Black">
<GridView.ItemTemplate>
<DataTemplate>
<Image Width="90" Height="90" Margin="5" Source="{Binding}" Stretch="UniformToFill"/>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
【问题讨论】:
-
有人对此有答案吗?我也在尝试创建一个画廊查看器,但如果我从一个文件夹中获取超过 100 张照片,则应用程序会因 System.OutOfMemoryException 而崩溃。
-
如果删除 AddMemoryPressure 调用会发生什么。我不确定你是否需要这样做。 AddMemoryPressure 命令的文档说明您需要在不需要时移除完全相同的压力量。如果不这样做,系统性能会受到影响。 msdn.microsoft.com/en-us/library/…
-
直接从路径加载图像到位图中怎么样?然后你可以使用 DecodePixel Width 和 Height 来约束它们。此外,如果用户只看到其中的一部分,请不要一次加载所有图片。
-
尝试使用并行方法,看看这篇文章。Parallel method
标签: c# windows-runtime windows-phone windows-phone-8.1