2

I'm facing memory problem with image loading in xamarin forms list view, now i need to implement bitmap image in forms but i cant do that in xamarin forms i can't find any namespace including bit map in xamarin forms i have a normal data binding code which also binds image too

 public async void GetResult()
    //public  void GetResult()
    {
        try
        {
            IsBusy = true;
            var client = new HttpClient();
            var json = await client.GetStringAsync(string.Format(Url));
            var items = JsonConvert.DeserializeObject<ObservableCollection<cm_main_category>>(json.ToString());
            foreach (var item in items)
            {
                 item.image_url = "http://somelink.net" + item.image_url.Substring(1, item.image_url.Length - 1);
               // item.image_url = "http://127.0.0.1" + item.image_url.Substring(1, item.image_url.Length - 1);
                ListItems.Add(item);
            }
            IsBusy = false;
        }

now i nee to implement bit map to this code what should be the approach ?

3 Answers3

1

If you want to save memory, you could try to use FFImageLoading CachedImage which is Image API compatible replacement with advanced memory caching and downsampling capabilities. Just replace Image with CachedImage and set one of its downsampling properties.

https://github.com/molinch/FFImageLoading

Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40
0

Obviously you want to bind the image by URI. That's pretty easy in Xamarin.Forms.

You simply need to bind the ImageSourceProperty of an ImageCell to the property that knows the URI. A great example for both XAML binding and C# binding can be found here: https://www.syntaxismyui.com/xamarin-forms-listview-imagecell-recipe/

EDIT: Copied from the comments: The solution is to set the image source to null and trigger the garbage collection as described here: OutOfMemoryError when loading an image

Community
  • 1
  • 1
Wosi
  • 41,986
  • 17
  • 75
  • 82
  • i can do that , and also i can populate listview with images also but the problem is i can't add more data to it because of memory error and i need the image to bitmap how can i implement bitmap image instead of normal image –  Aug 19 '15 at 11:45
  • What is the _memory error_ you are facing? Do you receive an `OutOfMemoryException`? Then read this http://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/ – Wosi Aug 19 '15 at 12:00
  • yes i am getting the OutOfMemoryException how can i implement it on forms that link is about native i need forms thank for your replay –  Aug 19 '15 at 12:17
  • I only know about memory issues reagarding images on android. Other users have already faced this problem. I think their solutions can help you 1. http://stackoverflow.com/questions/25807196/xamarin-forms-listview-outofmemeoryerror-on-android 2. http://stackoverflow.com/questions/31079620/outofmemoryerror-when-loading-an-image – Wosi Aug 19 '15 at 12:36
  • Also try to set Java heap size to eg. 1GB in Android Project options. I think it will fix your memory issues. – Daniel Luberda Aug 19 '15 at 13:30
  • i just collect the garbage then it works fine to me now thanks for your relay is there anything wrong with that did it harm to my app ? –  Aug 20 '15 at 09:08
  • That doesn't harm your app. You just trigger the removal of unused memory. If the question is answered, then please accept the answer. – Wosi Aug 20 '15 at 09:16
  • ** protected override void OnDisappearing() { BindingContext = null; Content = null; base.OnDisappearing(); GC.Collect(); }** –  Aug 20 '15 at 10:57
0
 protected override void OnDisappearing()
    {
        BindingContext = null;
        Content = null;
        base.OnDisappearing();
        GC.Collect();
    }

on some app this will help if your app is dealing with thousands of data with image better adding bitmap