【问题标题】:iOS - Reload UITableView AFTER AFNetworking Asynchronous Image Download Threads DoneiOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView
【发布时间】:2015-10-20 00:37:15
【问题描述】:

我正在尝试使用 AFNetworking 从 URL 异步下载图像,而不是在我的应用程序中一一下载,当您打开它时,主视图会获取您所有的朋友及其个人资料图片并加载 @987654322 @ 在每个单元格中包含您朋友的姓名、用户名和个人资料图片。这是我正在使用的代码:

[auth.loggedInUser getFriendsSuccessCallback:^(NSArray* friends){
    NSMutableArray *profilePictureRequests = [[NSMutableArray alloc] init];
    for (User* friend in friends) {
        if (![weakSelf.friendImageDictionary.allKeys containsObject:friend.userName])
        {
            if (!friend.profilePicture) {
                UIImage *profilePicture = [UIImage imageNamed:@"ProfilePic"];
                [weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName];
            }
            else
            {
                NSURL *profilePictureURL = [NSURL URLWithString:friend.profilePicture];
                NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureURL];
                AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
                requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
                [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, UIImage* profilePicture) {
                    NSLog(@"Response: %@", profilePicture);
                    if (profilePicture == nil)
                    {
                        profilePicture = [UIImage imageNamed:@"ProfilePic"];
                    }
                    [weakSelf.friendImageDictionary setObject:profilePicture forKey:friend.userName]; 
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    NSLog(@"Image error: %@", error);
                }];
                [profilePictureRequests addObject:requestOperation];
            }
        }
    }
    for (AFHTTPRequestOperation *requestOperation in profilePictureRequests)
    {
        [requestOperation start];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableView reloadData];
        weakSelf.dashboardViewController.loadingCoverView.hidden = YES;
        [weakSelf.dashboardViewController.tableActivityIndicator stopAnimating];
        NSLog(@"FINISHED LOADING ALL PROFILE PICTURES");
    });
}

问题是我的UITableView 在所有图像下载后台线程完成之前被重新加载,这导致UITableViewCells 中只有一些个人资料图片。我怎样才能等待所有这些都完成,然后我才能重新加载UITableView

另外,我很怀疑我是否正确地执行此操作。我这样做的方式(在这种情况下同时发生大约 30 个图像下载后台线程)不是 AFNetworking 的好习惯吗?假设一个用户有超过 100 个朋友,并且同时有超过 100 个图片下载后台线程发生,是否会导致未来的问题?

【问题讨论】:

    标签: ios objective-c uitableview uiimage afnetworking-2


    【解决方案1】:

    重新加载整个 TableView 可能会导致一些问题。当单元格出现在屏幕上时,您应该只更新单元格的 ImageView。这称为延迟加载,您无需下载可能永远不会看到的图像。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *identifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
        NSInteger currentTag = cell.tag + 1;
        cell.tag = currentTag;
    
        User *user = self.friends[indexPath.row];
    
        NSURL *profilePictureURL = [NSURL URLWithString:user.profilePicture];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:profilePictureURL];
    
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
        op.responseSerializer = [AFImageResponseSerializer serializer];
    
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, UIImage* image) {
    
            //Check that the cell hasn't been reused
            dispatch_async(dispatch_get_main_queue(), ^{
                if (cell.tag == currentTag) {
                    [cell.imageView setImage: image];
                }
            });
    
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //Error
        }];
    
    
        return cell;
    }
    

    我不记得了,但 AFNetworking 可能会为您缓存响应,如果没有,您可以随时自行调整,以免您再次下载图像。

    【讨论】:

    • 这非常有效!我所要做的就是将灰色圆圈作为占位符,当我向下滚动时,它们都很快变成了各自的头像。非常感谢!
    • 别担心,乐于助人
    【解决方案2】:

    如有疑问,请使用这样的框架:

    https://github.com/rs/SDWebImage https://github.com/path/FastImageCache

    他们会消除您正在做的所有事情,并会节省您的时间和金钱,让您的应用尽快进入 Appstore!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多