【问题标题】:Xamarin.forms OutOfMemory error when using images in listview在列表视图中使用图像时出现 Xamarin.forms OutOfMemory 错误
【发布时间】:2014-12-02 21:56:56
【问题描述】:

我有一个列表视图,我想显示从 URL 加载的每个项目的图像。如果我使用 ImageCell 作为 DataTemplate,它将加载一次,然后如果我再次尝试加载,则会收到 OutOfMemory 错误。

接下来我尝试使用 Xamarin 表单简介页面 http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/ 中的代码来创建自定义 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;
    }

}

但这不会加载甚至第一次加载列表

我尝试了 Avrohom 的代码(在此处找到 Xamarin.Forms ListView OutOfMemoryError exception on Android),但不幸的是它仍然无法在第一次加载。

有人有什么想法吗?

【问题讨论】:

  • 你的问题很不清楚。我看不到问题标题和描述之间的关系。
  • 抱歉 - 已编辑,不确定我是如何处理的

标签: listview out-of-memory xamarin.forms


【解决方案1】:

您的图像可能太大了,不是从文件大小的意义上说,而是这些文件的位图表示。

您应该使用较小的图像,或者实现一个自定义单元格渲染器,在运行时为您执行此操作。请参阅 Xamarin 文章 Load Large Bitmaps Efficiently

【讨论】:

  • 感谢您回答斯蒂芬。图片来自 URL,它们似乎都超过 500 像素。我该如何调整大小? (抱歉,对于 C# 和 Xamarin 来说非常新 - 几周后才新)。我知道如何实现自定义渲染器,但我不知道从哪里开始调整大小。干杯
  • listviews 中的 500px 图像确实非常大,而且检索也需要时间。为什么不在后端调整它们的大小?还是使用 cloudinary.com 之类的服务?
  • 感谢您的意见,斯蒂芬。这是一个 uni 项目,所以我们将不得不将它们排除在外,但我一定会记住它以备将来使用
  • 我有一个简单的网格,有 6 张图像 2x2 网格。图片是之前从 android 资源中加载的。但后来我将它作为嵌入式资源添加到 PCL 中。 Kn出现我设置了图像源并且ondisappearing我使图像源无效..但它仍然占用大量内存并且经常抛出内存不足错误。所以任何想法
猜你喜欢
  • 2023-04-10
  • 2016-08-28
  • 2011-10-21
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多