Load / Unload sprite sheet [SOLVED]

Hey,
I want to pre-load and unload a sprite sheet.

For loading I’m using
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist file);

this loads the plist and its texture as far as i know?

and for unloading i use

        SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(plist file);

but does this also unload the texture?, i can’t see any change for memory usage.

I don’t think it does.

Are you unloading because the textures are no longer needed at all? Or trying to save memory between usage of each texture?

I do call SpriteFrameCache::getInstance()->removeUnusedSpriteFrames(); in onTrimMemory in Android - I don’t know where to put it in iOS yet.

This way all unused sprites should be removed from memory. And my benefit is, that it can be triggered everytime Android needs more memory. I didn’t check the trim level, so it will always cleanup the memory.

1 Like

i just manually want to pre-load and unload specific spritesheets

But what does unused actually mean? everything that is not drawn on the screen?
Because i want to keep textures in memory even if they are not used at the moment.

I cite the doc:

/** Removes unused sprite frames.
 * Sprite Frames that have a retain count of 1 will be deleted.
 * It is convenient to call this method after when starting a new Scene.
 * @js NA
 */

This is fine for me, because all needed sprites will be loaded in init of the Scene. I don’t know why the count of 1 is the marker, but the dev team will have good reasons.

@slackmoehrle Do you know why the count of 1 is the trigger?

the thing is, i might have a popup for example, that is not drawn and used at the moment, but might be used if the play cicks on a button. and i don’t want to load this every time he clicks on a button, therefore i preload it when i start the scene that contains this popup.

So not sure if this function would just remove it if it is unused in the level at the point of calling the remove texture function?

I could just call it on every new(as suggested in the comments) scene before preloading everything, but that would cause problems when i go to another scene that uses this texture(popup) and cause unnecessary unloads/loads.

Thats why i wanna do it manually.

As I said. Don’t do it manually. Let the operating system call it for you: e.g. in Android -> onTrimMemory. So you can call the load everytime you open the dialog and if the data is still in memory, the call will do nothing and if the remove was triggered before, the images will be loaded.

Did I miss something important in your big picture?

ok I found my custom solution to this, by going throug the code where cocos adds plist sprite sheets to memory.
Below RemoveSpriteFramesFromFile just removes it from the internal loaded textures list, but not from memory, the rest just graps the plist texture data and with this data we can remove it from the texture cache, this works for me and unloads all the memory

std::string file = "xxxx.plist";

            SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
			std::string fullPath = FileUtils::getInstance()->fullPathForFilename(file);
			ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);

			std::string texturePath("");

			if (dict.find("metadata") != dict.end())
			{
				ValueMap& metadataDict = dict["metadata"].asValueMap();
				// try to read  texture file name from meta data
				texturePath = metadataDict["textureFileName"].asString();
			}

			Director::getInstance()->getTextureCache()->removeTextureForKey(FileUtils::getInstance()->fullPathFromRelativeFile(texturePath, file));
1 Like

In RootViewController.mm there is:
- (void)didReceiveMemoryWarning

1 Like

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