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

Having issues with large memory being consumed when displaying images

$
0
0

Hi,

I am currently working on a small project which requires some elements with images being displayed in a scrollview. I have a jpeg image of 43kb and when i try to display 5 instances of the my custom layout with the image about 10Mb of memory is consumed.

I am not sure the reason for this much of memory consumption. Can someone help?

I have attached below the sample code:

#region Enums

public enum FDataType   {
    FoodCatagoty,
    FoodType,
    Adultration
}

#endregion

#region WidgetTappedEventArgs Class

public class WidgetTappedEventArgs : EventArgs
{
    public object Data { get; set;  }
    public FDataType FdataType {get; set;}

    public WidgetTappedEventArgs (FDataType dataType, object data)
    {
        FdataType = dataType;
        Data = data;
    }
}

#endregion

#region widgetData Class

public class WidgetData: IDisposable
{
    public int ID { get; set; }
    public string BackgroundImage   { get; set; }
    public string Text { get; set;  }
    public FDataType FdataType { get; set;  }
    public object Data { get; set;  }

    #region IDisposable implementation

    public void Dispose ()
    {
        BackgroundImage = string.Empty;
        Text = string.Empty;
        Data = null;
    }

    #endregion
}

#endregion

#region ScrollWidget Class

public class ScrollWidget : ContentView, IDisposable
{
    private bool _selected = false;
    private TapGestureRecognizer tapGestureRecognizer;
    private WidgetData _data;
    private RelativeLayout _layout;

    public event EventHandler<WidgetTappedEventArgs> Tapped;

    public bool Selected
    {
        get{
            return _selected;
        }
        set{
            SelectedState (value);
            _selected = value;
        }
    }

    private void SelectedState(bool selectState)    {
        if (selectState) {
            _layout.BackgroundColor = Color.FromHex("#66CCFF");
        } else {
            _layout.BackgroundColor = Color.White;
        }
    }

    public ScrollWidget (WidgetData data, bool horizontal, double height)
    {
        double textHeight = height * .30;
        _data = data;

        _layout = new RelativeLayout () {
            Padding = 6,
            BackgroundColor = Color.White,
        };

        tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.Tapped += OnTapped;
        tapGestureRecognizer.Tapped += (s, e) => {
            EventHandler<WidgetTappedEventArgs> handler = Tapped;
            if (handler != null)    {
                handler(this, new WidgetTappedEventArgs(data.FdataType, data.Data));
            }
        };
        _layout.GestureRecognizers.Add(tapGestureRecognizer);

        var backgroundImage = new Image () { 
            Source = new FileImageSource () { File = data.BackgroundImage },
            Aspect = Aspect.AspectFill,
            InputTransparent = false,
            MinimumHeightRequest = height - textHeight,
            MinimumWidthRequest = height
        };


        _layout.Children.Add (backgroundImage, 
            Constraint.Constant (5), 
            Constraint.Constant (5),
            Constraint.Constant(height),
            Constraint.Constant(height - textHeight - 5));

        var dashlabel = new Label () {
            Text = data.Text,
            XAlign = TextAlignment.Center,
            YAlign = TextAlignment.Center,
            TextColor = Color.Black,
            FontFamily = Device.OnPlatform ("AvenirNextCondensed-Bold", "sans-serif-condensed", null),
            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
            InputTransparent = true
        };

        _layout.Children.Add (dashlabel, 
            Constraint.Constant (5), 
            Constraint.Constant(height - textHeight),
            Constraint.Constant(height),
            Constraint.Constant(textHeight));

        //          _frame.Content = _layout;
        Content = _layout;
        Content.BackgroundColor = Color.White;
    }

    protected void OnTapped(object sender, EventArgs e) {
        System.Diagnostics.Debug.WriteLine ("OnTapped in ScrollWidget...");
        EventHandler<WidgetTappedEventArgs> handler = Tapped;
        if (handler != null)    {
            handler(this, new WidgetTappedEventArgs(_data.FdataType, _data.Data));
        }
    }

    #region IDisposable implementation

    public void Dispose ()
    {
        _data.Dispose ();
        tapGestureRecognizer.Tapped -= OnTapped;
        _layout.GestureRecognizers.Remove (tapGestureRecognizer);
        tapGestureRecognizer = null;

        foreach (var item in _layout.Children) {
            if (item.GetType () == typeof(Image)) {
                //((Image)item).Source = null;
            } else if(item.GetType () == typeof(Label)) {
                ((Label)item).Text = string.Empty;
            }
        }
        _layout.Children.Clear ();
    }

    #endregion
}

#endregion

#region ContentLabel Class

public class ContentLabel :  Label
{
    public ContentLabel (string headerLabel)
    {
        XAlign = TextAlignment.Start;
        YAlign = TextAlignment.Center;
        Text = headerLabel;
        FontAttributes = FontAttributes.Bold;
        FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));
    }
}

#endregion

#region ScrollContainer Class

public class ScrollContainer : ContentView
{
    private ContentLabel _contentLabel;
    private ScrollView _scrollView;
    private StackLayout _stack;
    public double _height; 

    public ScrollContainer(string header, double height)    {
        _height = height;
        _contentLabel = new ContentLabel (header);

        _stack = new StackLayout {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        _scrollView = new ScrollView ();
        _scrollView.Orientation = ScrollOrientation.Horizontal;
        _scrollView.HorizontalOptions = LayoutOptions.FillAndExpand;
        _scrollView.Content = _stack;

        Content = new StackLayout {
            Children = {
                _contentLabel,
                _scrollView
            }
        };
    }

    public void AddControl()    {

        WidgetData widgetData = new WidgetData ();
        for (int i = 0; i < 5; i++) {
            widgetData.ID = 1;
            widgetData.BackgroundImage = "Water.jpeg";
            widgetData.Text = "Catagory";
            widgetData.FdataType = FDataType.FoodCatagoty;

            ScrollWidget widget = new ScrollWidget (widgetData, true, _height);

            _stack.Children.Add (widget);
        }
        GC.Collect ();
    }

    public void RemoveControl() {
        while (_stack.Children.Count > 0) {
            _stack.Children.RemoveAt (_stack.Children.Count - 1);
        }
    }
}

#endregion

#region ControllerView Class

public class ControllerView : ContentView
{
    private StackLayout _stack;
    private Button _button2, _button3;
    private ScrollContainer _scrollContainer1;

    public ControllerView(ScrollContainer scrollContainer1) {

        _scrollContainer1 = scrollContainer1;

        _button2 = new Button {
            Text = "Add Control",
        };
        _button2.Clicked += ButtonClicked;

        _button3 = new Button {
            Text = "Remove Control",
        };
        _button3.Clicked += ButtonClicked;

        _stack = new StackLayout {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = {
                _button2,
                _button3
            }
        };

        Content = _stack;
    }

    private void ButtonClicked(object sender, EventArgs e)  {
        string buttonName = ((Button)sender).Text;

        if (buttonName == "Add Control") {
            _scrollContainer1.AddControl ();
        } else {
            _scrollContainer1.RemoveControl ();
        }
    }
}

#endregion

#region TestPage Class

public class TestPage : ContentPage
{

    #region Private Members

    private RelativeLayout _layout;
    private ScrollContainer _container1;
    private ControllerView _controllerView;

    #endregion

    public TestPage ()
    {

        Title = "Types of food adultration";

        _layout = new RelativeLayout {

        };

        SizeChanged += OnPageSizeChanged;

        double height = Height;
        height = height - Device.GetNamedSize (NamedSize.Large, typeof(Label));
        height = height / 3;

        _container1 = new ScrollContainer ("Test", height);

        _controllerView = new ControllerView (_container1);

        _layout.Padding = new Thickness(15, 0, 15, 0);
        Content = _layout;

    }

    private void OnPageSizeChanged(object sender, EventArgs args)   {

        _layout.Children.Clear ();

        double height = Height;
        height = height - Device.GetNamedSize (NamedSize.Large, typeof(Label));
        height = height / 3;
        _layout.Children.Add (_container1,
            Constraint.Constant (0),
            Constraint.Constant (0),
            Constraint.Constant (Width),
            Constraint.Constant (height - 1));
        _container1._height = height;

        _layout.Children.Add (_controllerView,
            Constraint.Constant (0),
            Constraint.Constant (height),
            Constraint.Constant (Width),
            Constraint.Constant (height - 1));
    }
}

#endregion

I would really appreciate if someone could help me here as soon as possible as I am stuck with this issue and not able to give a demo to my client.

Thanks in advance.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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