ios - "UIImage imageWithContentsOfFile"在动画数组中不工作,我需要它来减少内存

标签 ios arrays animation uiimage

我的 View Controller 上有多个动画,当我播放它们时,内存变得非常高,大约为“450mb”,我一直在寻找阻止这种情况的方法。

我见过的一种方法是不使用;

[UIImage imageNamed:@"origami_panel"] 

加载图像,使用;
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010065"] ofType:@"png"]],

应该减少使用的整体内存。

然而,它没有吗?我的内存仍然上升到似乎与我对每张图像使用“imageNamed”时完全相同的数量。
我这个动画的完整代码和它的数组是这样的;
//Panel 1 Animation
    NSArray *panel1Frames;
    panel1Frames = [[NSArray alloc] initWithObjects:
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010001"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010002"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010003"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010004"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010005"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010006"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010007"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010008"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010009"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010010"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010011"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010012"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010013"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010014"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010015"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010016"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010017"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010018"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010019"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010020"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010021"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010022"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010023"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010024"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010025"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010026"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010027"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010028"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010029"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010030"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010031"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010032"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010033"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010034"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010035"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010036"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010037"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010038"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010039"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010040"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010041"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010042"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010043"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010044"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010045"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010046"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010047"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010048"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010049"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010050"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010051"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010052"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010053"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010054"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010055"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010056"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010057"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010058"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010059"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010060"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010061"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010062"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010063"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010064"] ofType:@"png"]],
                    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"origami_panel010065"] ofType:@"png"]],
                    nil];
    panel1Image.animationImages = panel1Frames;
    panel1Image.animationDuration = 2.6;
    panel1Image.animationRepeatCount = 1;

请有人能告诉我我有什么问题吗?或者也许有更好的方法来做到这一点?我厌倦了研究“dealloc”但找不到我需要的东西,也许这样会更好?
谢谢。

最佳答案

UIImage有一个名为 animatedImageNamed:duration: 的类方法.您提供图像的命名模式,例如 @"origami_panel0100"它会自动加载所有可用的图像。您应该使用它,因为它比单独加载每个都更优化。

例子:

假设您有这样命名的图像:

动画1.png
动画2.png
动画3.png
动画4.png
...
动画999.png

您可以通过以下方式创建动画序列:
UIImage* animatedImage = [UIImage animatedImageNamed:@"Anim" duration:20];
这将创建一个 UIImage代表动画的对象。您将此图像提供给您的 View ,它将以动画形式绘制。

文档 here 中有关此方法的更多信息.

关于ios - "UIImage imageWithContentsOfFile"在动画数组中不工作,我需要它来减少内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20713064/

相关文章:

ios - React Native 0.64 命令 PhaseScriptExecution 失败,退出代码非零

objective-c - Protobuf 与 JSON - Objective-C/iOS

javascript - 相同的代码在不同的设备上运行不同

PHP动态数组显示 double

c# - 使用套接字的文件传输 (C#) - 接收到的文件不包含完整数据

angularjs - 在Angular 1.4+中页面加载时ng-repeat交错动画

ios - UICollectionView 多重选择不会取消选择第一个单元格

ios - 如何用动画结束 UILongPressGestureRecognizer?

ios - 防止 UITableView 超出单元格绑定(bind) View 在 insertRowsAtIndexPaths : 上剪辑

ios - 如何使用 RxSwift 正确处理项目中的 Disposables?