2

これが私のコードです:ヘッダー:

#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

私は持っていViewControllerview、2つのボタンがあります。startAnimationアクションのある最初のボタン。これにより、が作成されUIImageViewNSMutableArrayアニメーションが開始されます。アクション付きの2番目のボタン。cleanMemoryこれは、で作成したものをすべてクリーンアップしstartAnimationます。楽器から始めるProfileと、プログラムに、ボタンを押すとアニメーションに変わり、アニメーションの後に ボタンを押しますが、同じです...なぜですか?開始値(4 mb Real Mem)までメモリをクリーンアップしません。どこに問題があるのか​​説明してもらえますか?Activity Monitor4 mb Real MemstartAnimation16 mb Real MemcleanMemory16 mb Real Mem

4

2 に答える 2

2

UIImage imageNamed:画像をキャッシュし、独自のスケジュールでメモリを解放します。キャッシュが必要ない場合は、メモリを完全に制御してから、イメージを直接ロードする必要がありますUIImage imageNamed:

アップルのドキュメントから:

このメソッドは、指定された名前のイメージ オブジェクトをシステム キャッシュで検索し、存在する場合はそのオブジェクトを返します。一致する画像オブジェクトがまだキャッシュにない場合、このメソッドは指定されたファイルから画像データをロードしてキャッシュし、結果のオブジェクトを返します。

使用できます

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

画像を直接ロードします。

アップルのドキュメントから:

このメソッドは、画像オブジェクトをキャッシュしません。

于 2012-01-20T20:34:45.017 に答える
1

使用し+ (UIImage *)imageWithContentsOfFile:(NSString *)pathてもメモリを解放できない場合は、tyr を呼び出してくださいimageView.animationImages = nil;

于 2012-01-20T21:52:21.580 に答える