Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Reduce the memory pressure of the UriImageSource cache

$
0
0

In my app, I have a very long ListView (with custom cells) that displays an image downloaded from the internet on each cell using UriImageSource. My custom cell is defined like that:

public class PostItemCell : ViewCell
  {
    public PostItemCell()
    {
      var grid = new Grid() 
      {    
        BackgroundColor = Color.Transparent,
        Padding = new Thickness(0.0, 0.0, 0.0, 0.0),
      };

      grid.ColumnDefinitions = new ColumnDefinitionCollection { 
        new ColumnDefinition 
        {
          Width = GridLength.Auto
        } 
      };

      var source = new UriImageSource
      {
        CachingEnabled = true,
        CacheValidity = new TimeSpan(5, 0, 0, 0),
      };
          source.SetBinding(UriImageSource.UriProperty, new Binding("Model.FeaturedImageUri"));

          var image = new Image
          {
            Source = source, Aspect = Aspect.AspectFill,
            WidthRequest = Constants.HOME_POST_CELL_HEIGHT * 1.5,
        HeightRequest = Constants.HOME_POST_CELL_HEIGHT,
        VerticalOptions = LayoutOptions.Start,
        HorizontalOptions = LayoutOptions.StartAndExpand,
      };
      grid.Children.Add(image, 0, 0);

      var stackLayout = new StackLayout() {
        Padding = 5,
        Spacing = 0
     };

      var titleLabel = new Label { 
        FontFamily = "Georgia",
        FontSize = 15.0f,
        TextColor = Color.Black,
        VerticalOptions = LayoutOptions.Center,
        LineBreakMode = LineBreakMode.WordWrap,
        HeightRequest = 35,
        YAlign = TextAlignment.Center,
        XAlign = TextAlignment.Start,
      };
      titleLabel.SetBinding(Label.TextProperty, new Binding("Model.Title"));
      stackLayout.Children.Add(titleLabel);

      var subTitleLabel = new Label { 
        FontFamily = "Arial",
        FontSize = 11.0f,
        VerticalOptions = LayoutOptions.Center,
        TextColor = Color.Gray,
        LineBreakMode = LineBreakMode.WordWrap,
        HeightRequest = 25,
        YAlign = TextAlignment.Center,
        XAlign = TextAlignment.Start,
      };
      subTitleLabel.SetBinding(Label.TextProperty, new Binding("Model.FormattedDate"));
      stackLayout.Children.Add(subTitleLabel);

      grid.Children.Add(stackLayout, 1, 0);

      View = grid;

    }

My issue is that the memory of the device (iPhone 6) increases a lot when scrolling and then I receive memory warnings and the app crash.

I was wondering:

  • There is a way to limit the memory space that the image cache can allocate like it was possible with the ImageLoader of MonoTouch.Dialog?
  • Or can we do something when a cell disappears to reduce the memory pressure?
  • Do we have less memory issues if we use the ImageCell class and then customize things with a custom cell renderer?

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>