0

我正在尝试做一个请求,但它只在第一次工作....

这是我的代码:

NSArray *infos = [rows objectAtIndex:indexPath.row];
NSString *thumbPath = thePath;

NSURLRequest *thumbRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbPath]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

self->thumbConnection = [[NSURLConnection alloc] initWithRequest:thumbRequest
                                                        delegate:self
                                                startImmediately:YES];

     self->thumbData = [[NSMutableData alloc]init];

收到响应时

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if (self->thumbData == nil) {
    self->thumbData = [[NSMutableData alloc]init];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

self.thumbImage.image = [UIImage imageWithData:self->thumbData];

self.thumbActivityView.hidden = YES;

self->thumbData = nil;
}

当 didFinishDownloading: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

self.thumbImage.image = [UIImage imageWithData:self->thumbData];

self.thumbActivityView.hidden = YES;

[self->thumbData release];

self->thumbData = nil;

}

当超时或其他错误方法时:

   - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self->thumbData release];
self->thumbData = nil;
self.thumbActivityView.hidden = YES;
[thumbConnection release];
self->thumbConnection = nil;

}
4

1 回答 1

0

如果它不是零,就不要再次分配它。最好还是在对象的 init 方法中分配它并在对象的 dealloc 方法中释放它

于 2013-01-03T14:33:33.163 回答