内存泄漏问题

Mad*_*tha 1 iphone

我正在UITableViewCell使用以下功能加载图像.但是当我用Debugger运行我的应用程序时,[pool release]每当我滚动我的时候它就会崩溃UITableView.我该怎么做才能解决这个问题?提前致谢.

- (void) loadImage{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    imageURL = [imageURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];      
    //NSLog(@"img url cell**** %@",imageURL);  
    self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
    //self.image = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];

    [pool release];
}
Run Code Online (Sandbox Code Playgroud)

fsa*_*int 5

事实上你使用的是NSAutoreleasePool,我猜这个加载映像是在一个不是主线程的线程中运行的.它是否正确?如果是这种情况,你在这个非主线程中进行UIKit调用(self.image = ...),这可能是你遇到的崩溃的可能来源.所有UIKit更新必须在主线程中进行,因为UIKit不是线程安全的.尝试更换:

self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
Run Code Online (Sandbox Code Playgroud)

通过

[self performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]] waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

注意我猜测setter的名称是setImage:如果setter选择器的名称不同,你可能需要更正.