OStack程序员社区-中国程序员成长平台

标题: ios - UIImageView动画后如何清理内存 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:51
标题: ios - UIImageView动画后如何清理内存

这是我的代码: 标题:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
    UIImageView *imageView;
    NSMutableArray *arrayWithImages;
}
- (IBAction)startAnimationid)sender;
- (IBAction)cleanMemoryid)sender;
@end

实现:

#import "ViewController.h"

@implementation ViewController

......

- (IBAction)startAnimationid)sender {
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

    arrayWithImages = [[NSMutableArray alloc] initWithObjects:
                                       [UIImage imageNamed"pic1"],
                                       [UIImage imageNamed"pic2"],
                                       [UIImage imageNamed"pic3"],
                                       [UIImage imageNamed"pic4"],
                                       [UIImage imageNamed"pic5"],
                                       [UIImage imageNamed"pic6"],
                                       [UIImage imageNamed"pic7"],
                                       [UIImage imageNamed"pic8"],nil];
    imageView.animationImages = arrayWithImages;
    imageView.animationDuration = 3;
    imageView.animationRepeatCount = 1;
    [self.view addSubview:imageView];

    [imageView startAnimating];
}

- (IBAction)cleanMemoryid)sender {

    [arrayWithImages removeAllObjects];
    [arrayWithImages release];
    arrayWithImages= nil;

    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;
}
@end

我有 ViewController 和它的 view 有两个按钮。第一个带有 startAnimation Action 的按钮,它创建 UIImageViewNSMutableArray 并在其上启动动画。第二个带有 cleanMemory Action 的按钮,用于清除我在 startAnimation 中创建的所有内容。 当我用 Activity Monitor 仪器启动 Profile 时,当我按下 startAnimation 按钮时,我的程序有 4 mb Real Mem它更改为 16 mb Real Mem 并且在动画之后我按下 cleanMemory 按钮,但它具有相同的 16 mb Real Mem...为什么?我不会将我的内存清理为起始值(4 mb Real Mem)。请问,你能解释一下我哪里有问题吗?



Best Answer-推荐答案


UIImage imageNamed: 缓存图像并按照自己的时间表释放内存。如果你不想缓存,那就是完全控制内存然后直接加载图像,而不是 UIImage imageNamed:

来自 Apple 文档:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

你可以使用

+ (UIImage *)imageWithContentsOfFileNSString *)path

直接加载图片。

来自 Apple 文档:

This method does not cache the image object.

关于ios - UIImageView动画后如何清理内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947389/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4