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

Den*_*nov 0 c# xaml out-of-memory windows-phone-8

当我尝试显示图像网格时,我在Windows手机上有"内存不足"异常.我玩了一点代码,认为这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;
     }
   }
Run Code Online (Sandbox Code Playgroud)

在另一个类中,我将这些图像添加到网格中(它是一系列的,由以前解析过的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);
    }
Run Code Online (Sandbox Code Playgroud)

猜一些评论:我已经将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>
       ....
Run Code Online (Sandbox Code Playgroud)

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

PS对不起误导.尺寸120x120是显示的矩形,加载的图像尺寸为800x800(例如),之后它们适合120x120矩形.

PPS如果我删除加载m_loader_bitmap,则图片显示正常.m_loader_bitmap是1px灰色png

Mau*_*hah 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;
        });
 }
Run Code Online (Sandbox Code Playgroud)

或者从这些链接中找到更多

BitmapImage导致内存泄漏

Windows Phone 8中BitmapImage/Image控件的内存消耗

WriteableBitmap内存泄漏

为System.OutOfMemoryException