UIImage loaded from URL in Xamarin / C#

Jason Hartley picture Jason Hartley · Jul 23, 2013 · Viewed 26.5k times · Source

It has been 4 years since this question has been answered with this blog post.

Is there a standard way to create a UIImage with an image from a URL? Something like:

UIImage image = UIImage.FromFile("http://foo.com/bar.jpg");

I feel like I'm probably missing something really simple.

Answer

poupou picture poupou · Jul 24, 2013

Not a one-liner, but with very few lines you can roll your own. E.g.

static UIImage FromUrl (string uri)
{
    using (var url = new NSUrl (uri))
    using (var data = NSData.FromUrl (url))
        return UIImage.LoadFromData (data);
}

The calls, including the one from UIImage, are thread-safe.