gpt4 book ai didi

ios - UIImageView动画太耗内存

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:38 25 4
gpt4 key购买 nike

我在制作动画时遇到内存和图像问题。首先,我正在使用 ARC。在我的初始屏幕上,我有大约 60 张要制作动画的图像。我将这段代码用于动画:

NSMutableArray *images = [[NSMutableArray alloc] init];
int animationImageCount = 61;
for (int i = 1; i < animationImageCount; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"s-%d", i]]];
}
self.fullAnimatedImage = [UIImage imageNamed:[NSString stringWithFormat:@"s-%d", animationImageCount - 1]];

self.animatedImageView.image = self.fullAnimatedImage;
self.animatedImageView.animationImages = [NSArray arrayWithArray:images];
self.animatedImageView.animationDuration = images.count/30;
self.animatedImageView.animationRepeatCount = 1;
[self.animatedImageView startAnimating];

// since there is no official callback method when UIImageView animation completes,
// we can just call a method after animation time has passed
[self performSelector:@selector(animationFinished) withObject:nil
afterDelay:self.animatedImageView.animationDuration];

问题是内存消耗大约是 300 MB,当然它在 iPhone 4 上崩溃。当我注释掉那部分代码时,内存消耗大约是 40 MB,没关系。我试过将 image 属性设置为 nil 什么也没有。我试图将它放在 @autoreleasepool {} 中,但它不会释放它的内容。您知道如何优化它吗?

最佳答案

您应该尝试将 [UIImage imageNamed:] 替换为 [UIImage imageWithContentsOfFile:]

来自文档:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

关于ios - UIImageView动画太耗内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180145/

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