【问题标题】:How to clean memory after UIImageView animationUIImageView动画后如何清理内存
【发布时间】:2012-01-20 20:20:27
【问题描述】:

这是我的代码: 标题:

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

实施:

#import "ViewController.h"

@implementation ViewController

......

- (IBAction)startAnimation:(id)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)cleanMemory:(id)sender {

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

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

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

【问题讨论】:

    标签: ios animation memory-management uiimageview xcode-instruments


    【解决方案1】:

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

    来自 Apple 文档:

    此方法在系统缓存中查找具有 指定名称并返回该对象(如果存在)。如果一个匹配 图像对象尚未在缓存中,此方法加载图像 来自指定文件的数据,缓存它,然后返回 结果对象。

    你可以使用

    + (UIImage *)imageWithContentsOfFile:(NSString *)path
    

    直接加载图片。

    来自 Apple 文档:

    此方法不缓存图像对象。

    【讨论】:

      【解决方案2】:

      如果即使在使用+ (UIImage *)imageWithContentsOfFile:(NSString *)path 之后您仍然无法释放内存,请致电imageView.animationImages = nil;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多