0

在我的WP7應用程序中,我從Web上下載了200個圖像並保存在獨立存儲中。當調試所有圖像時,按隊列方法加載全景視圖,並且我可以查看它何時連接到PC。斷開它與PC斷開後,我打開應用程序和瀏覽圖像它顯示一些圖像,並終止。當大量圖像顯示時應用程序崩潰

if (i < 150) 
    { 

     WebClient m_webClient = new WebClient();    
     Uri m_uri = new Uri("http://d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_" + i + "/mobile_high.jpg"); 
     m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
     m_webClient.OpenReadAsync(m_uri); 

    }   

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
    int count; 

    try 
    { 
     Stream stream = e.Result;    
     byte[] buffer = new byte[1024]; 

     using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 

      //isf.Remove(); 

      using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("IMAGES" + loop2(k) + ".jpg", FileMode.Create, isf)) 
      { 
       count = 0; 

       while (0 < (count = stream.Read(buffer, 0, buffer.Length))) 
       { 
        isfs.Write(buffer, 0, count); 
       } 

       stream.Close(); 
       isfs.Close(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
} 
+0

你能給我們一些關於你使用isolationStorage的方式的代碼嗎? – ChapMic 2012-03-08 12:30:22

+0

在真實設備上運行呃? – 2012-03-08 12:39:01

+0

是隻在真實的設備中..我已經加載全景視圖中的圖像首先添加了3個圖像從iso商店和使用選擇更改事件刪除第一個圖像,並添加第四個圖像vicecersa .. – 2012-03-08 12:41:27

回答

0

在Skype上交談之後,我回顧了他的代碼,發現他的問題與他的Isolated Storage Explorer有關。它無法連接到他的電腦,所以它給了一個錯誤。與圖片加載無關。

1

我認爲你的問題是,如果您加載一次太多的圖像在一個循環中,你出去循環的時刻,讓焦點回到UI線程所有的垃圾收集的位圖圖像是完成。

This article解釋它更好一點,並提供一個解決方案。

我也有這個問題,並提出了我自己的解決方案。我有一個需要加載圖片url的dictonairy,但是你可以很容易地改變你的場景。

This SO question也是關於這個問題(加載多個圖像和崩潰(異常))。它也有微軟對此的迴應,我根據他們的迴應我的解決方案。

在我的解決方案中,我使用調度程序返回到UI線程,從而確保使用的圖像和位圖的垃圾被清理。

private void LoadImages(List<string> sources) 
{ 
    List<string>.Enumerator iterator = sources.GetEnumerator(); 
    this.Dispatcher.BeginInvoke(() => { LoadImage(iterator); }); 
} 

private void LoadImage(List<string>.Enumerator iterator) 
{ 
    if (iterator.MoveNext()) 
    { 
     //TODO: Load the image from iterator.Current 

     //Now load the next image 
     this.Dispatcher.BeginInvoke(() => { LoadImage(iterator); }); 
    } 
    else 
    { 
     //Done loading images 
    } 
} 
+0

你說沒有需要在這裏使用iso存儲..爲我的應用程序它的必須爲我有一個本地目錄和文件路徑 – 2012-03-08 12:53:20

+0

我沒有說,我說你可以很容易地改變我的解決方案,以適應你的情況。 – SynerCoder 2012-03-08 12:58:34

+0

我可以重寫我的代碼來適應你的問題,但是我需要知道你如何循環它。 'loop2(k)'做了什麼?什麼是'k'?什麼是實際加載循環? – SynerCoder 2012-03-08 13:00:13

0

我會非常警惕一次加載200張圖片的內存影響。你是否在分析內存使用情況?使用太多內存可能會導致您的應用程序被終止。

相關問題