[[CCTextureCache sharedTextureCache] textureForKey: @ "myImageName"]; return nil

I have a lot of large images, when the device has not only my application in memory, my application does something strange: I used

[[CCTextureCache sharedTextureCache] addImage:@"myImageName"];


to download all the images. I have some sprites that change their texture with:

[[CCTextureCache sharedTextureCache] textureForKey:@"myImageName"];


everything goes well at the beginning of the game, all images are displayed. but the more I play the more sprites, becoming white. sharedTextureCache automatically deletes sprites in warning memory? in RootViewController I haven't changed anything and looks like this:

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


why can't i find textures?

Is this a cocos2d problem? or the correct way to do it:

CCTexture2d *tex=[[CCTextureCache sharedTextureCache] textureForKey:@"myImageName"];
if(!tex) [[CCTextureCache sharedTextureCache] addImage:@"myImageName"];
[mysprite setTexture:tex]




source to share

1 answer

There is a method in CCDirector.m purgeCachedData


-(void) purgeCachedData
    [CCLabelBMFont purgeCachedData];
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];


and a typical AppDelegate.m created from the cocos2d template has:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    [[CCDirector sharedDirector] purgeCachedData];


The result will be called removeUnusedTextures

of CCTextureCache

, which eliminates all the textures in the cache, which is only stored by the cache.



All Articles