【问题标题】:NSArray leaking with a lot of imagesNSArray 泄漏大量图像
【发布时间】:2010-10-17 00:17:36
【问题描述】:

我正在使用 Tom Brow 在 GitHub 上提供的 LeavesView 类。

在 iPad 上,我有 23 张 1024x768 的图像,大小在 200-500KB (JPG) 之间。我在不损失质量的情况下尽可能多地压缩图像。出于某种原因,当我使用图像列表进行初始化时,内存显着下降,最终崩溃。这是一些代码:

- (id)init {
    if (self = [super init]) {
        images = [[[NSArray alloc] initWithObjects:
              [UIImage imageNamed:@"001.jpg"],
              [UIImage imageNamed:@"002.jpg"],
              [UIImage imageNamed:@"003.jpg"],
              [UIImage imageNamed:@"004.jpg"],
              [UIImage imageNamed:@"005.jpg"],
              [UIImage imageNamed:@"006.jpg"],
              [UIImage imageNamed:@"007.jpg"],
              [UIImage imageNamed:@"008.jpg"],
              [UIImage imageNamed:@"009.jpg"],
              [UIImage imageNamed:@"010.jpg"],
              [UIImage imageNamed:@"011.jpg"],
              [UIImage imageNamed:@"012.jpg"],
              [UIImage imageNamed:@"013.jpg"],
              [UIImage imageNamed:@"014.jpg"],
              [UIImage imageNamed:@"015.jpg"],
              [UIImage imageNamed:@"016.jpg"],
              [UIImage imageNamed:@"017.jpg"],
              [UIImage imageNamed:@"018.jpg"],
              [UIImage imageNamed:@"019.jpg"],
              [UIImage imageNamed:@"020.jpg"],
              [UIImage imageNamed:@"021.jpg"],
              [UIImage imageNamed:@"022.jpg"],
              [UIImage imageNamed:@"cover.jpg"],
              nil] autorelease];
    }
    return self;
}

现在,在这段代码中,我使用了自动释放。我也试过:

-(void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    NSLog(@"memory warning");
    LeavesCache *lv =[LeavesCache alloc];
    [lv flush];
    [lv release];
    //[images release];
}

当然,在这个例子中,我只使用图像而不是我在视图中使用的其他对象:

-(void)viewDidUnload {
    [super viewDidUnload];
    images = nil;
}
-(void)dealloc {
    [images release];
    [super dealloc];
}

当我对图像数组使用自动释放时,[图像释放];被注释掉,反之亦然,如果我不使用 autorelease,我会在 dealloc 中使用 release。

现在,当我弹出视图时,内存按预期恢复。只是图像在使用时似乎是一个 HOG,我不知道不使用图像数组的另一种方法。

任何帮助将不胜感激。

【问题讨论】:

    标签: image ipad memory crash nsarray


    【解决方案1】:

    我能够通过以下方式解决我的 emory 泄漏问题:

    [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"001" ofType:@"jpg"]]
    

    我看到使用这种方法恢复了大量的内存,并且能够停止崩溃,希望这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      相关资源
      最近更新 更多