我有一组 URL,用于使用 NSURLConnection 从远程服务器接收相关图像
NSURL *url = [NSURL URLWithString:URLString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest
requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:2.0f];
// Run network request asynchronously
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
在委托协议中
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// As chuncks of the image are received, we build our data file
[self.imageData appendData:data];
}
最后
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// All data has been downloaded, now we can set the image in the images array
_img = [UIImage imageWithData:self.imageData];
[self.imagesArray insertObject:_img atIndex:_index];
[self.imageData setLength:0];
_index = _index+ 1;
}
但接收的顺序并不总是与发送 URL 请求的顺序相同我尝试使用实例变量索引在图像数组中以相同的顺序插入这些图像,但这并不总是有效,我仍然得到无序列表,谁能指出我如何实现这一目标的正确方向。