为什么这个UIImage永远不会更新,即使它引用的文件发生了变化?

时间:2012-03-05 15:11:14

标签: objective-c ios cocos2d-iphone

所以,我正在一个视图中创建一个UIImage(Cocos2d游戏的屏幕截图),如下所示:

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

稍后,在另一个屏幕上,我在Cocos2d菜单中显示所有屏幕截图,如下所示:

-(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];

}

但是,如果以后更换屏幕截图,它在此视图中不会更新,即使我可以告诉此视图每次加载时都会调用[self showGardens] new。如果我完全退出应用程序(并强制退出它,或其他),然后再次打开它,屏幕截图是正确的。

是某种内存问题吗?或者应用程序在运行时以某种方式缓存所有图像,并且只在完成时重新加载?

图像存储在以下位置:

  

的/ var /移动/应用/ 77DE5CC9-93DE-4346-A8AE-89F38036F718 /文档/样品   Garden17.png

1 个答案:

答案 0 :(得分:1)

因为cocos2d在加载纹理时会缓存纹理。在重新加载文件之前,您需要从缓存中删除纹理。

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