Image NSArray泄漏了大量图像
我正在使用Tom Brow在GitHub上提供的LeavesView类 在iPad上,我有23张图像,大小为1024x768,范围从200-500KB(JPG)。我已经尽可能多地压缩了图像,但没有丢失质量。出于某种原因,当我初始化图像列表时,内存会显著下降,最终崩溃。下面是一些代码:Image NSArray泄漏了大量图像,image,ipad,memory,crash,nsarray,Image,Ipad,Memory,Crash,Nsarray,我正在使用Tom Brow在GitHub上提供的LeavesView类 在iPad上,我有23张图像,大小为1024x768,范围从200-500KB(JPG)。我已经尽可能多地压缩了图像,但没有丢失质量。出于某种原因,当我初始化图像列表时,内存会显著下降,最终崩溃。下面是一些代码: - (id)init { if (self = [super init]) { images = [[[NSArray alloc] initWithObjects:
- (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
现在,当我弹出视图时,内存将按预期恢复。只是图像在使用时看起来像一只猪,我不知道不使用图像阵列的其他方法
任何帮助都将不胜感激。我能够通过以下方式解决我的埃默里漏洞:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"001" ofType:@"jpg"]]
我看到通过使用这种方法恢复了大量的内存,并且能够阻止这些错误,希望这能帮助其他人