gpt4 book ai didi

ios - 即使使用 ARC 和 imageFilePath,动画也会因内存而终止

转载 作者:行者123 更新时间:2023-11-28 22:14:08 25 4
gpt4 key购买 nike

在我的应用程序加载并显示其启动屏幕后,我正在制作一个简单的 png 动画。动画在模拟器中有效,但在实际 iPhone 中无效,它因内存压力而终止(虽然启动屏幕确实首先加载)。在调试中,内存以指数方式增加到 200MB,直到它崩溃。仪器显示没有泄漏,所有堆和匿名 Vm 总体为 9.61 MB。动画在实际 iPhone 上根本不显示。

我已经停止使用 imageNamed 设置动画图像,并按照另一个帮助主题的建议使用 imageFilePath。它工作并且图像加载到模拟器上。我只是不确定还能做什么。非常感谢您的帮助。

在 didFinishLaunchingWithOptions 中:

[self.window makeKeyAndVisible];
self.animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
NSArray *animationArray = [[NSArray alloc] initWithObjects:
[UIImage imageFromMainBundleFile:@"/Splash_00000.png"],
[UIImage imageFromMainBundleFile:@"/Splash_00001.png"],

//大约有 150 个,所以我将省略其余部分

                                      nil];

self.animationView.animationImages = animationArray;
self.animationView.animationDuration = 5.0f;
self.animationView.animationRepeatCount = 1;
[self.animationView startAnimating];
[self.window addSubview:self.animationView];
[self.window bringSubviewToFront:self.animationView];

如果需要,这是我从另一个线程获得的方法:

+(UIImage*)imageFromMainBundleFile:(NSString*)aFileName
{
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, aFileName]];
}

最佳答案

撇开启动画面不是 recommended ,你没有泄漏,但你用完了堆(这就是它在模拟器上工作的原因)。

这些图像中的每一个都归数组所有,并且在动画完成很久之后才会被 ARC 释放。请记住,PNG 在磁盘上压缩时,将在内存中解压缩。

解决方案 - 有几个浮现在脑海中。

  1. 将动画分成一系列离散的阶段并确保在每个阶段之间发布图像(使用 @autoreleasepool)
  2. 更现实一点,将动画渲染成电影并播放相反(不太可能在阶段之间口吃)

关于ios - 即使使用 ARC 和 imageFilePath,动画也会因内存而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180070/

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