gpt4 book ai didi

image - NSArray泄漏了很多图像

转载 作者:行者123 更新时间:2023-12-03 17:43:20 25 4
gpt4 key购买 nike

我正在使用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];
}

当然,在此示例中,我仅使用图像,而不使用 View 中使用的其他对象:
-(void)viewDidUnload {
[super viewDidUnload];
images = nil;
}
-(void)dealloc {
[images release];
[super dealloc];
}

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

现在,当我弹出 View 时,内存将按预期方式重新获得。只是图像在使用时似乎是HOG,我不知道另一种不使用图像数组的方法。

任何帮助将不胜感激。

最佳答案

我能够通过以下方式解决我的Emory泄漏:

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

我看到使用此方法恢复了可观的内存,并能够阻止折痕,希望这对其他人有所帮助!

关于image - NSArray泄漏了很多图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com