gpt4 book ai didi

ios - 应用程序从后台恢复后,如何防止 UIImage 消失? (iPad)

转载 作者:行者123 更新时间:2023-11-28 20:45:45 26 4
gpt4 key购买 nike

我有一个 UIImage* loadedPoofSprite,我是这样初始化的:

NSMutableDictionary* loadedDictionary=[NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",Main_Bundle_Path,poofConfigFileName]];
NSString *textureFileName = [[loadedDictionary objectForKey:@"metadata"] valueForKey:@"textureFileName"];
loadedPoofSprite = [UIImage imageNamed:textureFileName];

poofImage = [[UIImage imageNamed:textureFileName] CGImage];

从那里我使用我的 poofImage,一切似乎都很好,直到我的应用程序从后台恢复。我崩溃了,因为 loadedPoofSprite 不见了。我认为避免这种情况的方法是像这样保留 loadedPoofSprite:

[loadedPoofSprite autorelease];
loadedPoofSprite = [[UIImage imageNamed:textureFileName] retain];

这行得通,但有人建议我这样做不是正确的方法,我基本上应该在 applicationWillEnterForeground 中再次加载 loadedPoofSprite。我不明白为什么加载它两次而不是保留它更好。处理上述问题的正确方法是什么?

谢谢!

最佳答案

如果您要使 loadedPoofSprite 成为保留属性,并像这样分配它:

self.loadedPoofSprite = [UIImage imageNamed:textureFileName];

那么一切都会好起来的。保留的属性使用

声明
@property (nonatomic,retain) UIImage *loadedPoofSprite;

你需要使用

生成一个getter/setter
@synthesize loadedPoofSprite;

然后在dealloc例程中,添加self.loadedPoofSprite = nil;,在需要的时候释放图片。

关于ios - 应用程序从后台恢复后,如何防止 UIImage 消失? (iPad),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6484763/

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