【问题标题】:Download images to UICollectionView via AFNetworking通过 AFNetworking 将图像下载到 UICollectionView
【发布时间】:2015-02-22 00:17:35
【问题描述】:

我是 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 中,之后我会使用它。 谢谢你读到。

///////////////////////////////更新//////// ///////////////////////////p>

我找到了解决方案。但这并不能完全满足我。图像随机出现。如何让它们按顺序加载?

- (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];
    }
}

【问题讨论】:

    标签: ios objective-c xcode afnetworking get-request


    【解决方案1】:

    您需要从 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]]];
    

    //添加新数据后重新加载集合视图

    【讨论】:

    • 我认为[_collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];[_collectionView reloadData]; 更好
    • 是的,这样会更有效率,尤其是当您的图像集增加时 - 我编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多