Downloading image in Cocos Creator 3.x

Hi,
I have a simple question how to get downloaded image in CC 3.2? From https://docs.cocos.com/creator/3.2/api/en/classes/asset_manager.downloader.html#download I copied this code but it does not work:

assetManager.downloader.download(user.photoURL,'.png', {onFileProgress: 
(loaded, total) => console.log(loaded/total)}, onComplete: (err) => console.log(err));

How to get an image from url? Also if the image is downloaded as an object (for example:
var image = downloadedImage) it is deleted when the next scene is loaded or it is saved in asset folder and accessible later?

It would be deleted as its not saved to disk. This is in memory.

1 Like

Use this.

export function GetSpriteFromUrl(url: string)
{
    return new Promise((resolve, reject) =>
    {
        assetManager.loadRemote<ImageAsset>(url, { xhrMimeType: "image/png" }, (error, imageAsset) =>
        {
            if (!error)
            {
                const spriteFrame = new SpriteFrame();
                const texture = new Texture2D();
                texture.image = imageAsset;
                spriteFrame.texture = texture;

                resolve(spriteFrame);
            }
            else
            {
                reject(null);
            }
        });
    });
}
3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.