gpt4 book ai didi

objective-c - 为什么这个 UIImage 从不更新,即使它引用的文件发生变化?

转载 作者:行者123 更新时间:2023-11-29 13:39:30 25 4
gpt4 key购买 nike

所以,我在一个 View 中创建一个 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];

}

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

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

图像存储在如下位置:

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

最佳答案

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

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

关于objective-c - 为什么这个 UIImage 从不更新,即使它引用的文件发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9568963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com