此代码中显示的图像正在泄漏,但我无法弄清楚如何。我有一个显示要显示的图像的tableview。每次用户选择图像时,它应删除旧图像,下载新图像,然后将其添加到滚动视图。但旧图像没有被释放,我无法弄清楚为什么......
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[imageView removeFromSuperview];
self.imageView = nil;
NSUInteger row = [indexPath row];
NSString *tempC = [[NSString alloc]initWithFormat:@"http://www.website.com/%@_0001.jpg",[pdfNamesFinalArray objectAtIndex:row] ];
chartFileName = tempC;
pdfName = [pdfNamesFinalArray objectAtIndex:row];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *tempString = [[[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath,pdfName]autorelease];
NSData *data = [NSData dataWithContentsOfFile:tempString];
if (data != NULL){
self.imageView = nil;
[imageView removeFromSuperview];
self.imageView = nil;
UIImageView *tempImage = [[[UIImageView alloc]initWithImage:[UIImage imageWithData:data]]autorelease];
self.imageView = tempImage;
[data release];
scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 1;
scrollView.minimumZoomScale = .6;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
scrollView.zoomScale = .37;
}
else {
[data release];
self.imageView = nil;
[imageView removeFromSuperview];
self.imageView = nil;
activityIndicator.hidden = NO;
getChartsButton.enabled = NO;
chartListButton.enabled = NO;
saveChartButton.enabled = NO;
[NSThread detachNewThreadSelector:@selector(downloadImages) toTarget:self withObject:nil];
}
chartPanel.hidden = YES;
}
-(void) downloadImages {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
self.imageView = nil;
[imageView removeFromSuperview];
NSURL *url = [[[NSURL alloc]initWithString:chartFileName]autorelease];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *tempImage = [[[UIImageView alloc]initWithImage:[UIImage imageWithData:data]]autorelease];
self.imageView = tempImage;
tempImage = nil;
scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 1;
scrollView.minimumZoomScale = .37;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
scrollView.zoomScale = .6;
activityIndicator.hidden = YES;
getChartsButton.enabled = YES;
chartListButton.enabled = YES;
saveChartButton.enabled = YES;
[pool drain];
[pool release];
}
答案 0 :(得分:7)
这看起来不对:
self.imageView = nil;
[imageView removeFromSuperview];
在将<{em}>从中删除之前,你将imageView
设置为nil ,因此第二个语句实际上只是[nil removeFromSuperview];
,它不会做任何事情。< / p>
答案 1 :(得分:3)
我认为泄漏是David Gelhar所说的,但我只想补充一点,你不应该从主线程以外的线程访问UI内容(例如,不要在{1}下执行单独的线程)。这可能会导致非常奇怪的问题,包括神秘的泄漏。尝试将所有内容放在您使用[imageView removeFromSuperview]
调用的主线程上的单独方法中,并查看它是否仍然泄漏。
另外(虽然这不会导致泄漏),[self performSelectorOnMainThread:]
会释放自动释放池,所以你不应该在它之后调用[pool drain]
- 它可能会释放主线程上的池,可能会在某个地方造成崩溃(因为你可能会过度释放游泳池)。