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

ios - 如何停止在表格 View 中重复加载图像?

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

这是我显示来自服务器的图像的代码。加载图像后,当您向下滚动然后返回时,图像会消失,并且在再次加载之前还有另一个延迟。

  dispatch_async(kBgQueue, ^{

        NSString *profileImageStr = [[displayItems objectAtIndex:indexPath.row] objectForKey"image_url"];
        NSURL* profileImgURL = [NSURL URLWithString:profileImageStr];
        NSData *profileImgData = [NSData dataWithContentsOfURL:profileImgURL];


        NSString *postPhotoStr = [[displayItems objectAtIndex:indexPath.row] objectForKey"image_url"];
        NSURL *postImgURL = [NSURL URLWithString:postPhotoStr];
        NSData *postImageData = [NSData dataWithContentsOfURL:postImgURL];


        dispatch_async(dispatch_get_main_queue(), ^{


            if (postImageData && profileImgData != nil) {


                cell.thumbnailImg.image = [UIImage imageWithData:profileImgData];




                cell.bgImgView.image = [UIImage imageWithData:postImageData];
            }else
            {


                cell.thumbnailImg.image = [UIImage imageNamed"noimage.jpg"];




                cell.bgImgView.image = [UIImage imageNamed"noimage.jpg"];
            }

        });

    }
                   );
    return cell;



Best Answer-推荐答案


我认为您需要为图像实现缓存。将图像存储在 NSCache 中。

//instance 
NSCache *imageCache;


- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
     NSString *simpleTableIdentifier=nil;
    if([objRepresentative.team count]>0)
    {
        simpleTableIdentifier=[NSString stringWithFormat"%@",[objRepresentative.employeeID objectAtIndex:indexPath.row]];
    }
    else
        simpleTableIdentifier = @"ContactTableCell";


     ContactTableCell *cell = (ContactTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed"ContactTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        NSString *photoString=[NSString stringWithFormat"http://www.foo.com/%@",[searchPhoto objectAtIndex:indexPath.row]];

            UIImage *image = [imageCache objectForKey:photoString];
            if (image)
            {
                cell.ProfilePicture.image=image;
            }
            else
            {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{


                    NSURL *url = [NSURL URLWithString:[photoString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

                    if(image)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            UIImage *small = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
                            cell.ProfilePicture.image=small;
                        });

                        [imageCache setObject:image forKey:photoString];
                    }
                    else
                        cell.ProfilePicture.image=[UIImage imageNamed"noPicture.png"];

                });
            }
    }
return cell;
}

关于ios - 如何停止在表格 View 中重复加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19216721/

回复

使用道具 举报

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

本版积分规则

关注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