Windows Phone图像网格"内存不足"错误

时间:2014-11-12 23:05:53

标签: c# xaml windows-phone-8 out-of-memory

当我尝试显示图像网格时,Windows手机上出现“Out of memory”异常。 我玩了一些代码并认为Grid.Children.Add给了我那个错误。

我已经制作了显示灰色矩形的自定义类,并且在加载图像后,它显示的是图像而不是该矩形。这是:

   class ImageWIthLoader
   {
     private BitmapImage m_loader_bitmap;
     private BitmapImage m_source_bitmap;
     private Image m_image;

     public Image image
     {
         get { return m_image; }
     }

     //url - url to 120x120 image; width, height = 120 both
     public ImageWIthLoader(string url, int width, int height)
     {
         m_image = new Image();

         m_loader_bitmap = new BitmapImage(new Uri("Assets/Images/image_await_background.png", Uri Kind.Relative));
         m_loader_bitmap.DecodePixelWidth = width;
         m_loader_bitmap.DecodePixelHeight = height;

         m_image.Source = m_loader_bitmap;

         m_source_bitmap = new BitmapImage(new Uri(url));
         m_source_bitmap.CreateOptions = BitmapCreateOptions.None;
         m_source_bitmap.ImageOpened += bitmapLoaded;
     }

     private void bitmapLoaded(object sender, RoutedEventArgs e)
     {
         m_image.Source = m_source_bitmap;
         m_loader_bitmap.UriSource = null;
     }
   }

在另一个类中,我将这些图像添加到网格中(它是一系列图像,由之前解析过的json的url加载)

    private void addImageToGrid(ImageWIthLoader loader_image, int row, int col)
    {
        double margin = 0.05 * PIC_WIDTH;

        loader_image.image.SetValue(Grid.ColumnProperty, col);
        loader_image.image.SetValue(Grid.RowProperty, row);
        loader_image.image.Margin = new Thickness(margin);

        //comment out this string and everythins goes fine
        images_grid.Children.Add(loader_image.image);
    }

猜测一些评论:我已将ImageWithLoader-s保存到List<>防止GC收集它们。 ImageWithLoader网址是一个120x120图像的网址,所以它们一定不会那么重。

包含图像网格的页面的XAML的一部分:

       ....
        <ScrollViewer x:Name="scrollView" HorizontalAlignment="Left" Height="603" VerticalAlignment="Top" Width="456" >
            <Grid x:Name="images_grid" Height="596" Width="453"/>
        </ScrollViewer>
       ....

那么,拜托,我做错了什么?我应该使用另一个控件而不是网格吗?或者还有其他我想念的东西?

P.S。抱歉误传。尺寸120x120是显示的矩形,加载的图像尺寸为800x800(例如),之后它们适合120x120矩形。

P.P.S。如果我删除加载m_loader_bitmap,则图片显示正常。 m_loader_bitmap是1px grey png

1 个答案:

答案 0 :(得分:5)

您可以像这样更改此代码

public void ImageWIthLoader(string url, int width, int height)
 {
      Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
     m_image = new Image();

     m_loader_bitmap = new BitmapImage(new Uri("Assets/Images/image_await_background.png", UriKind.Relative));
     m_loader_bitmap.DecodePixelWidth = width;
     m_loader_bitmap.DecodePixelHeight = height;

     m_image.Source = m_loader_bitmap;

     m_loader_bitmap = null;

     m_source_bitmap = new BitmapImage(new Uri(url));
     m_source_bitmap.CreateOptions = BitmapCreateOptions.None;
     m_source_bitmap.ImageOpened += bitmapLoaded;
        });
 }

或从这些链接中找到更多

Memory leak with BitmapImage

Memory consumption of BitmapImage/Image control in Windows Phone 8

WriteableBitmap Memory Leak

System.OutOfMemoryException