Search code examples
iosxamarinffimageloading

Using FFImageLoading Synchronously?


I need to use the LoadUrl method of the FFImageLoading ImageService class synchronously since it is called in a CreateCell method of a data source for a grid. Using Xamarin.iOS and c#. Appreciate a quick solution. Here is the current code:

public override IGFlowLayoutViewCell CreateCell(IGFlowLayoutView flowLayoutView, nint index)
        {
            IGFlowLayoutViewCell cell = flowLayoutView.DequeueReusableCell("CELL") as IGFlowLayoutViewCell;
            UIImageView iconImage = new UIImageView();

            if (cell == null)
            {
                cell = new IGFlowLayoutViewCell("CELL");
                cell.ContentInset = new UIEdgeInsets(2, 2, 2, 2);
                cell.ContentMode = UIViewContentMode.Center;
                cell.Layer.BorderColor = UIColor.LightGray.CGColor;
                cell.Layer.BorderWidth = 1;

                cell.ContentView = iconImage;

                ImageService.LoadUrl(instrumentImageUrls[index], new TimeSpan(12, 0, 0))
                            .Into((UIImageView)cell.ContentView,0);
            }

            return cell;
        }

Solution

  • public override IGFlowLayoutViewCell CreateCell(IGFlowLayoutView flowLayoutView, nint index)
            {
                IGFlowLayoutViewCell cell = flowLayoutView.DequeueReusableCell("CELL") as IGFlowLayoutViewCell;
                UIImageView iconImage = new UIImageView();
    
                if (cell == null)
                {
                    cell = new IGFlowLayoutViewCell("CELL");
                    cell.ContentInset = new UIEdgeInsets(2, 2, 2, 2);
                    cell.ContentMode = UIViewContentMode.Center;
                    cell.Layer.BorderColor = UIColor.LightGray.CGColor;
                    cell.Layer.BorderWidth = 1;
    
                    cell.ContentView = iconImage;
    
                    ImageService.LoadUrl(instrumentImageUrls[index], new TimeSpan(12, 0, 0))
                                .IntoAsync((UIImageView)cell.ContentView,0).GetAwaiter().GetResult();
                }
    
                return cell;
            }