【问题标题】:Application crashes when lots of images are displayed显示大量图像时应用程序崩溃
【发布时间】:2012-03-09 00:41:20
【问题描述】:

在我的 WP7 应用程序中,我从 Web 下载了 200 张图像并保存在隔离存储中。调试时,所有图像都通过队列方法加载到全景视图中,我可以在连接到 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());
    }
}

【问题讨论】:

  • 你能给我们一些你使用isolatedStorage的代码吗,...?
  • 是的,仅在真实设备中..我已经在全景视图中加载了图像,首先从 iso 商店添加了 3 张图像,并使用选择更改事件删除了第一张图像并添加了第四张图像。

标签: silverlight-4.0 windows-phone-7.1 windows-phone-7 isolatedstorage


【解决方案1】:

我认为您的问题是,如果您在一个循环中一次加载太多图像,那么当您退出循环并将焦点返回给 UI 线程时,位图图像上的所有垃圾收集都已完成。

This article 解释得更好,并提供了解决方案。

我也遇到了这个问题,并想出了自己的解决方案。我有一个需要加载的图像 url 字典,但您可以根据自己的场景轻松更改它。

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
    }
}

【讨论】:

  • 你是不是说这里不需要使用iso store..对于我的应用来说,我必须有一个本地目录和文件路径
  • 我没有这么说,我说您可以轻松更改我的解决方案以适应您的情况。
  • 我可以重写我的代码以适应你的问题,但我需要知道你如何循环它。 loop2(k) 是做什么的? k 是什么?实际的加载循环是什么?
  • 它只是将整数值从 0 到 150 一个一个地递增。图像 url 是串行方式d1mu9ule1cy7bp.cloudfront.net/2012//pages/p_0/mobile_high.jpgd1mu9ule1cy7bp.cloudfront.net/2012//pages/p_1/…
  • 是的,用于使用此方法递增 public int loop2(int i) { k = i + 1;返回 k - 1; } 你也可以使用 for 循环
【解决方案2】:

在 Skype 上交谈后,我查看了他的代码,发现他的问题出在隔离存储资源管理器上。它无法连接到他的电脑,所以它给出了一个错误。与图片加载无关。

【讨论】:

    【解决方案3】:

    我会非常警惕一次加载 200 张图像对内存的影响。您是否一直在分析内存使用情况?使用过多的内存可能会导致您的应用程序被终止。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多