受苹果的LazyTables示例的启发,我刚刚为自己构建了一个名为ImageLoader的类。我太聪明了。当我非常聪明的时候,我经常会遇到这样的情况,在如何处理这个物体的记忆方面,我遇到了我智慧的极限。
它是从viewDidLoad中的UIViewController子类实例化的,因此:
ImageDownloader *downloader = [[ImageDownloader alloc] init];
[downloader startDownloadWithImageView:self.theImageView
andImageURL:[NSURL URLWithString:myUrlString]];
在ImageDownloader的方法中,我说:
-(void)startDownloadWithImageView:(UIImageView *)imageView
andImageURL:(NSURL *)url
{
self.theImageView = imageView;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.activeDownload = [NSMutableData data];
NSURLConnection *conn = [[NSURLConnection alloc]
initWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
self.imageConnection = conn;
[conn release];
}
作为NSURLConnectionDelegate,我的委托方法是(在相关部分):
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
theImageView.image = image;
[image release];
}
所以问题是。。。如何释放
downloader
autorelease
从我创建它的ViewController,因为我不希望NSURLConnection在run循环结束时调用它的委托。我可以释放吗
self
connectionDidFinishLoading
? 我从来没有见过这样做,觉得有点奇怪,释放自己在一个自己的方法。
我的另一个想法是,我可以让它成为一个单独的,只是不担心它会一直存在,直到我们终止。但我宁愿让这个物体复活,然后干干净净地离开。
顺便说一句,我聪明的下一步是将此功能添加为UIImageView的一个类别。打电话来会很方便的
[imageView asynchronouslyLoadImageFromURL:(NSURL *)url]
.