我是 iOS 编程新手。所以我有新手问题。我开始使用 AFNetworking 2,这就是任务:
我有一个请求。它的响应是第二个请求的一部分。这意味着我必须等到第一个请求结束。他们循序渐进。当我得到第二个响应时,我对其进行解析并以http://lalala-xx.jpg格式保存 20 个 URL 。之后我想加载图像并将它们放入 UICollectionView 中,并且我不想在范围内而是在方案“下载->直接到单元格”中进行。我将 URL 和图像保存在单例类中并像访问它们一样访问它们
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.imageView.image = [[UserData sharedUser].images objectAtIndex:indexPath.row];
return cell;
}
方法链看起来像
- (void)method1
{
NSString *string = [NSString stringWithFormat:firstRequestString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
// getting needed part for second request
[self method2:(NSString *)part1];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[operation start];
}
第二种方法:
- (void)method2:(NSString *)part1
{
// lalala making secondRequestString
NSString *string = [NSString stringWithFormat:secondRequestString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSMutableArray *imageURLs = [[NSMutableArray alloc] init];
// getting needed URLs
[self loadAllImages:(NSMutableArray *)imageURLs];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[operation start];
}
最后的:
- (void)loadAllImages:(NSMutableArray *)imageURLs
{
// ???
}
我被困住了。接下来我该怎么办?我有 20 个 URL,但我应该如何下载图像并将它们定向到 ViewController 以更新单元格中的图像?我想 AFNetworkig 可以为我提供一些操作队列。
而且我现在不喜欢我的代码。我使用这个链,但我想要一个独立的方法 2 返回 imgURL。所以它应该看起来:用户按下按钮->方法1->方法2->停止。等到用户按下按钮 -> 下载 image1 -> 显示 image1 -> 下载 image2 -> 显示 image2 -> 等等 -> 下载 imageN -> 显示 imageN -> 停止。我再说一遍,我需要将图像存储在 Array 中,之后我将使用它。谢谢你读到。
///////////////////////////////////// 更新 //////////// ///////////////////////
我找到了解决方案。但这并不能完全满足我。图像随机出现。如何让它们按顺序加载?
- (void)loadAllImages:(NSMutableArray *)imageURLs
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
for (NSURL *url in imageURLs)
{
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFImageResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
[[UserData sharedUser].images addObject:responseObject];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CollectionViewRealoadData" object:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// show error
}];
[manager.operationQueue addOperation:operation];
}
}