• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:33:48 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap