How to load an image asynchronously from URI in Xamarin forms

13,465

Solution 1

Grab the data asynchronously and assign it to your Source once it is done.

System.Uri uri;
System.Uri.TryCreate(imageURI, UriKind.Absolute, out uri);
Task<ImageSource> result = Task<ImageSource>.Factory.StartNew(() => ImageSource.FromUri(uri));
_companyImage.Source = await result;

Solution 2

You can simply set the Image.Source property to a URI and let Xamarin.Forms do the work for you (per "Working with Images" in Xamarin.Forms docs).

Example

var someImage = new Image() {
    Aspect = Aspect.AspectFit,
    Source = ImageSource.FromUri(new Uri("http://xamarin.com/content/images/pages/branding/assets/xamagon.png")),
};
Share:
13,465
koreus737
Author by

koreus737

Updated on June 04, 2022

Comments

  • koreus737
    koreus737 about 2 years

    Here is my current code for loading the Image:

    System.Uri uri;
    System.Uri.TryCreate (imageURI, UriKind.Absolute, out uri);
    _companyImage.Source = ImageSource.FromUri (uri);
    

    The problem is that the whole program has to wait for this task to complete, I would like to load the image asynchronously but I can't work out how to create an image source asynchronously.