Download image from URL and apply to Sprite component

I’ve been searching on how to download images from an URL to a cocos creator game, i have no succeeded at all, because either the topic on the forum contains obsolete code or the thing wasnt solved, and through the things I have tried the only one that got me close to achieve that was changing the sprite frame of the sprite component, but that does not seems to work as well.It loads the image as a Texture2D, but when i try to set it to a new SpriteFrame it throws an uncaught reference error “Uncaught TypeError: n.textureLoaded is not a function”. So if anyone could help me that I would appreciate it

1 Like

Try this (typescript):

cc.loader.load(url, (err, tex)=>{
this.picSprite.spriteFrame = new cc.SpriteFrame(tex);
});

More info: http://www.cocoscreator.com/docs/creator/api/classes/loader.html

1 Like

it didnt worked, ir doesnt return any texture2d, I followed an example on the documentation link

That loader code worked for me to fetch facebook profile image of Instant game

i see, well must be a problem with my link then… do you know any way to load the contents of the file from the URL and then apply the contents ona new texture2d, and then after set it to a spriteframe?

1 Like