0

I have a list view that I want to display an image for each item, loaded from a URL. If I use an ImageCell as the DataTemplate, it will load once and then if I try to load again I will get an OutOfMemory error.

I next tried the code from Introduction to Xamarin Forms page here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/ to create a custom ViewCell

class MyViewCell : ViewCell
{
    public MyViewCell()
    {
        var image = new MyImage
        {
            HorizontalOptions = LayoutOptions.Start
        };
        image.SetBinding(Image.SourceProperty, new Binding("ImagePath"));
        image.WidthRequest = image.HeightRequest = 40;

        var nameLayout = CreateLayout();

        var viewLayout = new StackLayout()
        {
            Orientation = StackOrientation.Horizontal,
            Children = { image, nameLayout }
        };
        View = viewLayout;
    }//end constructor

    static StackLayout CreateLayout()
    {

        var titleLabel = new Label
        {
            HorizontalOptions= LayoutOptions.FillAndExpand,
            TextColor = Color.FromRgb(0, 110, 128)
        };
        titleLabel.SetBinding(Label.TextProperty, "Title");

        var detailLabel = new Label
        {
            HorizontalOptions = LayoutOptions.FillAndExpand
        };
        detailLabel.SetBinding(Label.TextProperty, "Detail");

        var layout = new StackLayout()
        {
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Orientation = StackOrientation.Vertical,
            Children = { titleLabel, detailLabel }
        };
        return layout;
    }

}

But that won't load the even load the list a first time

I tried the Avrohom's code (found here Xamarin.Forms ListView OutOfMemoryError exception on Android), but unfortunately it still won't load the first time.

Does anyone have any ideas?

Community
  • 1
  • 1
user1667474
  • 819
  • 6
  • 24
  • 37

1 Answers1

2

Your images are probably way too big, not in the file-size sense, but the bitmap representation of those files.

You should use smaller images, or implement a custom cell renderer that does that for you at runtime. See the Xamarin article Load Large Bitmaps Efficiently.

lindydonna
  • 3,874
  • 17
  • 26
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
  • Thanks for answering Stephane. The images are from URL's, and they all do seem to be 500px plus. How do I go about resizing? (Sorry, extremely new to C# and Xamarin - as in a few weeks new). I know how to implement a custom renderer, but I don't know where to start for the resizing. Cheers – user1667474 Oct 08 '14 at 12:37
  • 500px images in listviews are indeed extremely huge, and takes time to retrieve as well. why don't you resize them on your backend ? Or use a service like cloudinary.com ? – Stephane Delcroix Oct 08 '14 at 15:03
  • Thanks for your input, Stephane. This is for a uni project, so we will have to leave them out, but I will defnitely keep it in mind for the future – user1667474 Oct 09 '14 at 00:38
  • I have a simple grid with 6 images 2x2 grid. Images were loaded from android resources earlier. But then i added it into PCL as embedded resources. Kn appearing i set the imagesource and ondisappearing i make the imagesource nul.. but still it occupie lot of memory and outofmemory error is frequently thrown. So any ideas – SoftSan Jan 14 '15 at 11:52