How do you download image to UI.Image?

This is as far as I got

IEnumerator downloadImg (string url){
		WWW www = new WWW(url);
		yield return www;
		Texture2D texture = new Texture2D(1, 1);
		www.LoadImageIntoTexture (texture);

		Sprite sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));
		imagePrefabClone.GetComponent<Image>().sprite = sprite;
	}

But for some reason the sprite is not getting set. Any ideas?

This got it working

IEnumerator isDownloading(string url){
		// Start a download of the given URL
		var www = new WWW(url);			
		// wait until the download is done
		yield return www;
		// Create a texture in DXT1 format
		Texture2D texture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
		
		// assign the downloaded image to sprite
		www.LoadImageIntoTexture(texture);
		Rect rec = new Rect(0, 0, texture.width, texture.height);
		Sprite spriteToUse = Sprite.Create(texture,rec,new Vector2(0.5f,0.5f),100);
		imageToDisplay.sprite = spriteToUse;

		www.Dispose();
		www = null;
	}

You can do it easily with Davinci

The library has a simple usage and supports Unity UI.Image and 3D model textures.

Davinci.get().load(imageUrl).into(image).start();

Hope this helps!

I thinks helpful for you.

//here is the code

string WebUrl , ServerUrl;
public Image Web_image , ServerImage;
Texture2D tex;
WWW Link;

IEnumerator LoadImageInternet()
{
	tex = new Texture2D(4, 4, TextureFormat.DXT1 , false);
	Link = new WWW (WebUrl);
	yield return Link;
	Link.LoadImageIntoTexture (tex);
	Web_image.sprite = Sprite.Create (tex, new Rect (0, 0, tex.width, tex.height), new Vector2 (0, 0)); 
}

//Button Event
public void LoadFromInternet()
{
	WebUrl = "https://upload.wikimedia.org/wikipedia/commons/d/dc/Cats_Petunia_and_Mimosa_2004.jpg";
	StartCoroutine (LoadImageInternet ());
}

Now in 2021 Unity recommend this code that is more efficient and also worked perfectly.

//PlayGamesPlatform.Instance.GetUserImageUrl() Used to get player Image from Google Play Games

using UnityEngine.Networking;

    private IEnumerator GetPlayerImage(string url)
        {
            UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
            yield return www.SendWebRequest();
    
            Texture2D myTexture = DownloadHandlerTexture.GetContent(www);
    
            Rect rec = new Rect(0, 0, myTexture.width, myTexture.height);
            Sprite spriteToUse = Sprite.Create(myTexture, rec, new Vector2(0.5f, 0.5f), 100);
    
            playerImage = spriteToUse;
        }