OGeek|极客世界-中国程序员成长平台

标题: iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:33
标题: iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView

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

[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"rofilePic"];
                [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"rofilePic"];
                    }
                    [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 在所有图像下载后台线程完成之前被重新加载,这导致 UITableViewCell 中只有一些个人资料图片。我如何才能等待所有这些都完成,然后我才能重新加载 UITableView

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



Best Answer-推荐答案


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

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 可能会为您缓存响应,如果没有,您可以随时自行调整,以免您再次下载图像。

关于iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33226171/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4