我是iOS编程的新手。所以我有新手问题。我开始使用AFNetworking 2,这就是任务:
我有一个请求。它的回应是第二个请求的一部分。这意味着我必须等到第一个请求结束。他们一步一步地遵循。当我得到第二个回复时,我会解析它并以http://lalala-xx.jpg格式保存20个网址。之后,我想加载图像并将它们放入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返回imgURLs。所以应该看: 用户按下按钮 - > method1 - > method2 - >停。等到直到用户按下按钮 - >下载image1 - > show image1 - >下载image2 - > show image2 - >等等 - >下载imageN - > show imageN - >停。我重复一遍,我需要将图像存储在数组中,之后我将使用它。 你读过那个。
/////////////////////////////////////更新///////// ////////////////////////////
我找到了解决方案。但它并不能完全满足我。图像随机出现。如何使它们按顺序加载?
- (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];
}
}
答案 0 :(得分:0)
您需要从URL获取数据,然后从该数据创建UIImage对象。
您可以使用NSURL方法从URL获取数据
for(NSString *imgString in imageURLs){
NSURL *url = [NSURL URLWithString:imgString];
NSData *imgData = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:imgData ];
[imageUrls addObject:img];//this mutable array should be initialized early in view controller life cycle (like ViewDidLoad).
}
获得图像对象后,可以将其添加到用作集合视图的数据源的图像阵列中。
[_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[imageUrls count] - 1 inSection:0]]];
//添加新数据后重新加载您的集合视图