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?

2
  • your question is very unclear. I can't see the relation between the question title and the description. Commented Oct 8, 2014 at 11:44
  • Sorry - have edited, not sure how I managed that Commented Oct 8, 2014 at 12:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.