【问题标题】:Animation Terminating due to Memory even with ARC and imageFilePath即使使用 ARC 和 imageFilePath,动画也会因内存而终止
【发布时间】:2014-04-06 11:12:45
【问题描述】:

在我的应用加载并显示其启动屏幕后,我正在制作一个简单的 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]];
}

【问题讨论】:

  • “大约有 150 个”。那就是你的问题。

标签: ios objective-c xcode animation memory


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢!我很感激答案!我现在要把它渲染成电影。
猜你喜欢
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多