问题描述
在我的Windows Phone 8的应用程序,我有一个 LongListSelector
这势必有一个属性1000个对象的列表 base64string 图像。现在展示形象,我写了这个转换器转换的
bas64string
到流
。
In my Windows Phone 8 application I have a page with a LongListSelector
which is bound to a list of 1000 objects having a property for base64string
for image. Now to display the image, I wrote this converter to convert the bas64string
into a 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;
}
}
和这是的MemoryUsage:
And this is the memoryusage:
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
目前内存等于或者一些字节比这更大的156368896个字节,应用程序崩溃与 EngineExecutionException
。一旦我得到了OutOfMemoryException异常为此:
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);
显然这是内存的问题,我需要清除图片缓存的内存,但我怎么看这个答案http://stackoverflow.com/a/12267163/1949475 ,但我无法使用它。
Obviously this is a memory issue. I need to clear image cache memory but how? I see the link in this answer http://stackoverflow.com/a/12267163/1949475, but I'm unable to use it.
请注意:不是所有的图像都在同一时间显示,我回去再回到页面再次更改为显示在LongListSelector数据后的应用程序需要这么多内存
Note: not all images are displayed at the same time, and the application takes this much memory after I go back and come back to the page again changing the data to be displayed in the LongListSelector.
推荐答案
在您的转换器类设置是很重要的。
It is important to set in your converter class
BitmapImage image = new BitmapImage();
image.DecodePixelType = DecodePixelType.Logical;
image.CreateOptions = BitmapCreateOptions.BackgroundCreation;
image.CreateOptions = BitmapCreateOptions.DelayCreation;
image.DecodePixelWidth = 56;
image.DecodePixelHeight = 100;
这篇关于如何免费的Windows Phone 8的图像缓存/内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!