gpt4 book ai didi

c# - 如何在 Windows Phone 8 中释放图像缓存/内存?

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:22 25 4
gpt4 key购买 nike

在我的 Windows Phone 8 应用程序中,我有一个包含 LongListSelector 的页面,该页面绑定(bind)到一个包含 1000 个对象的列表,该对象具有用于图像的 base64string 属性。现在为了显示图像,我编写了这个转换器来将 bas64string 转换为 stream

public object Convert(object value, Type targetType, object parameter,  System.Globalization.CultureInfo culture)
{
if (!value.ToString().Contains("http://"))
{
string str = value.ToString();
byte[] bytes = Converter.FromBase64String(str);

using (MemoryStream stream = new MemoryStream(bytes))
{
stream.Seek(0, SeekOrigin.Begin);

BitmapImage image = new BitmapImage();
image.SetSource(stream);
bytes = null;
var memoryusage = string.Format("Memory: {0} bytes",
DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));
Debug.WriteLine(memoryusage);
return image;
}
}
else
{
return null;
}
}

这是内存使用情况:

    Memory: 92549120 bytes    Memory: 92946432 bytes    Memory: 92946432 bytes    Memory: 92946432 bytes    Memory: 92946432 bytes    Memory: 93192192 bytes    Memory: 93192192 bytes    Memory: 96079872 bytes    Memory: 100700160 bytes    Memory: 100700160 bytes    Memory: 109568000 bytes    Memory: 111734784 bytes    Memory: 142852096 bytes    Memory: 143056896 bytes    Memory: 143056896 bytes    Memory: 143261696 bytes    Memory: 140791808 bytes    Memory: 141103104 bytes    Memory: 141529088 bytes    Memory: 142151680 bytes    Memory: 146784256 bytes    Memory: 146784256 bytes    Memory: 155066368 bytes    Memory: 156368896 bytes

At memory equals to or maybe some bytes greater than this 156368896 bytes, the application crashes with the EngineExecutionException. Once I got "OutOfMemoryException for this:

image.SetSource(stream);

显然这是内存问题。我需要清除图像缓存内存,但如何?我在这个答案中看到了链接 https://stackoverflow.com/a/12267163/1949475 ,但我无法使用它。

注意:并非所有图像都同时显示,应用程序在我返回并再次返回页面更改要在 LongListSelector 中显示的数据后占用这么多内存。

最佳答案

在你的转换器类中设置很重要

BitmapImage image = new BitmapImage(); 
image.DecodePixelType = DecodePixelType.Logical;
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
image.CreateOptions = BitmapCreateOptions.DelayCreation;
image.DecodePixelWidth = 56;
image.DecodePixelHeight = 100;

关于c# - 如何在 Windows Phone 8 中释放图像缓存/内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22102212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com