Use of ImageLoader in MonoTouch - wrong image loaded when having large lists of images

StackOverflow https://stackoverflow.com//questions/12680324

  •  12-12-2019
  •  | 
  •  

Frage

I have a list of countries in a tableview (about 100 rows). If I scroll through it at a slow pace, everything works fine and the correct images are loaded. If I scroll through as fast as I can, then some of the country flags gets mixed up. It seems like a bug in the ImageLoader but I find it strange that nobody else has encountered it? I am using the latest code from here: https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Utilities/ImageLoader.cs

If I change the "MaxRequests" in ImageLoader.cs to a much larger value like 30 everything seems to work. If I change it down to 3 everything loads very slowly and it shows a lot of wrong country flags.

Is there something wrong with the way I use this class? I tried going the subclass way like here: https://github.com/xamarin/mobile-samples/blob/master/MWC/MWC.iOS/UI/CustomElements/SpeakerCell.cs

but I got exactly the same problem. Worked fine when scrolling slowly, and got confused when scrolling fast.

I load the image like this:

private System.Collections.Generic.Dictionary<string, List<NSIndexPath>> listOfImageUrls = new System.Collections.Generic.Dictionary<string, List<NSIndexPath>>();

public void UpdatedImage (Uri uri)
{
    InvokeOnMainThread (() => ReloadImage(uri));
}

public void ReloadImage(Uri uri){
    String id = uri.ToString();
    if(!listOfImageUrls.ContainsKey(id))
        return;
    List<NSIndexPath> indexes = listOfImageUrls[id];
   _tvc.GetTableView().ReloadRows(indexes.ToArray(), UITableViewRowAnimation.None);
}

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
 ...
   UIImageView imgLogo = ...; 
   string imageUri = getCountryFlagUrlSmall (dataSourceItemCountry);
   imgLogo.Image = MonoTouch.Dialog.Utilities.ImageLoader.DefaultRequestImage (new Uri (imageUri), this);           

   if(listOfImageUrls.ContainsKey(imageUri))
   {
      if(!listOfImageUrls[imageUri].Contains(indexPath))
        listOfImageUrls[imageUri].Add(indexPath);                       
   }
   else
    listOfImageUrls.Add(imageUri, new List<NSIndexPath>{indexPath});
   } 
...
War es hilfreich?

Lösung

I changed over to use this one: https://github.com/sichy/UrlImageStore/blob/master/MonoTouch.UrlImageStore/UrlImageStore.cs

Required only 4-5 lines of code to change implementation and everything loads correctly.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top