J.R.
J.R.

Reputation: 6049

Why does this UIImage never update, even when the file it references changes?

So, I am creating a UIImage (a screenshot of a Cocos2d game) in one view like so:

    [UIImagePNGRepresentation(screenshot) writeToFile:savePath options:NSDataWritingAtomic error:&error];

Later, on another screen, I display all screenshots in a Cocos2d menu like so:

-(void) showGardens{

    CCMenu* menu = [[CCMenu alloc] initWithItems:nil vaList:nil];
    menu_items = [NSMutableArray arrayWithCapacity:gardens.count];
    for(Garden* garden in gardens){
        NSLog(@"Making menu item for garden %@", garden.name);
        NSString *loadPath = [NSHomeDirectory() stringByAppendingPathComponent:garden.screen_shot_name];
        CCMenuItem* save_button;
        NSFileManager*  fileManager = [NSFileManager defaultManager];
        if([fileManager fileExistsAtPath:loadPath]){
            save_button = [CCMenuItemImage itemFromNormalImage:loadPath 
                                                             selectedImage:loadPath target:self selector: @selector(loadGarden:)];
        }else{
            save_button = [CCMenuItemImage itemFromNormalImage:@"default_screenshot.png" 
                                                             selectedImage:loadPath target:self selector: @selector(loadGarden:)];
        }


        [save_button autorelease];
        CCLabelTTF *label = [CCLabelTTF labelWithString:garden.name fontName:@"Marker Felt" fontSize:100];

        // position the label on the center of the screen
        label.position =  ccp(300,555);
        ccColor3B color = {0,0,0};
        [label setColor: color]; 
        [save_button addChild: label];      
        save_button.scale = 0.35;
        [menu_items addObject: save_button];

    }
    //make sure its not just overwriting one child.    
    for(CCMenuItem* menu_item in menu_items){
        [menu addChild: menu_item];
        [menu_items retain];
    }
    [menu alignItemsHorizontallyWithPadding:80.0];
    menu.position = ccp(700,500);
    [self addChild: menu];

}

However, if a screenshot gets replaced later on, it doesn't update in this view, even though as far as I can tell this view calls [self showGardens] new each time it is loaded. If I exit out of the app entirely (and force quit it, or whatever) then open it up again, the screenshots are correct.

Is it some sort of memory issue? Or does the application somehow cache all images while its running, and only reloads when done?

The image is stored in locations like:

/var/mobile/Applications/77DE5CC9-93DE-4346-A8AE-89F38036F718/Documents/Sample Garden17.png

Upvotes: 1

Views: 144

Answers (1)

Lukman
Lukman

Reputation: 19164

Because cocos2d caches textures as it loads them. You need to remove the texture from the cache before reloading the file.

[[CCTextureCache sharedTextureCache] removeTextureForKey:loadPath];
save_button = [CCMenuItemImage itemFromNormalImage:loadPath 
                                     selectedImage:loadPath 
                                            target:self 
                                          selector:@selector(loadGarden:)];

Upvotes: 1

Related Questions