在我的Windows Phone 8應用程序中,我有一個帶有LongListSelector
的頁面,該頁面綁定到1000個對象,該對象具有用於圖像的base64string
屬性。現在爲了顯示圖像,我寫了這個轉換器來將bas64string
轉換成stream
。如何在Windows Phone 8中釋放圖像緩存/內存?
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:
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異常:
image.SetSource(stream);
顯然,這是內存問題,我需要清晰的圖像緩存,但我怎麼看到這個答案https://stackoverflow.com/a/12267163/1949475的聯繫,但我無法使用它。?。
注意:不是所有的圖像都在同一時間顯示,我回去後,回來的頁面再次改變數據顯示在LongListSelector的應用程序需要這麼多內存
你*處置從'轉換'返回的圖像的地方? –
否....圖像返回到xaml中的圖像元素 –