2014-02-28 55 views
1

在我的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的應用程序需要這麼多內存

+0

你*處置從'轉換'返回的圖像的地方? –

+0

否....圖像返回到xaml中的圖像元素 –

回答

4

這是重要的設置在您的轉換器類

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

你能幫我在這裏http://stackoverflow.com/questions/24157246/wp8-out-of-memory-error-while-loading-images – Goofy

+0

Isn這是一個臨時解決方案嗎?我的意思是你正在減少一個位圖圖像消耗的內存,但如果我加載大量的圖像,不會出現這個問題。 – Raj123