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

ios - SDWebImage 线程占用 CPU

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

这是我的问题:

我有一个包含自定义 UITableViewCellUITableView。这些 UITableViewCell(称为 HomePicCell)中的每一个都与一个 Pic 对象相关联,该对象具有指向图像 URL 的属性。 一旦显示我的单元格,我就开始使用 SDWebImage manager 下载此图像。

一切正常,但在 20 到 80 秒后,一些线程开始占用 CPU。然后,该设备成为那些寒冷冬夜的完美手暖器,但我现在宁愿跳过这个功能!

我无法确定导致此问题的原因。我不认为保留循环会成为问题,因为它只会占用内存。一个经过试验的意见会很有帮助。

hogging threads

这是我的代码:

UITableView 数据源

- (UITableViewCell*)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    NSString* cellIdentifier = [@"HomePicCell" stringByAppendingString:[Theme sharedTheme].currentTheme];
    HomePicCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[HomePicCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    if(self.pics.count>0){
        Pic* pic = self.pics[indexPath.section];
        [cell configureWithPic:pic];
    }

    return cell;
}

HomePicCell (configureWithPic

- (void)configureWithPicPic*)pic
{
    self.pic = pic;

    // Reinit UI
    [self.progressView setHidden:NO];
    [self.errorLabel setHidden:YES];
    [self.photoImageView setAlpha:0];
    [self.progressView setProgress:0];

    [self.pic downloadWithDelegate:self];
}

图片

- (void) downloadWithDelegateid<icDownloadDelegate>)delegate
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    [manager downloadWithURL:self.url options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {

        if(expectedSize>0){
            float progress = [@(receivedSize) floatValue]/[@(expectedSize) floatValue];
            [delegate pic:self DownloadDidProgress:progress];
        }

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {

        self.isGif = @(image.images.count>1);

        if(image){
            if(cacheType == SDImageCacheTypeNone){
                [delegate pic:self DownloadDidFinish:image fromCache:NO];
            }else{
                [delegate pic:self DownloadDidFinish:image fromCache:YES];
            }
        }else{
            [delegate pic:self DownloadFailWithError:error];
        }

    }];
}

HomePicCell(委托(delegate)方法)

- (void)picPic*)pic DownloadDidFinishUIImage *)image fromCacheBOOL)fromCache
{
    if(![pic isEqual:self.pic]){
        return;
    }

    [self.progressView setHidden:YES];
    self.photoImageView.image = image;
    [self updateUI];
}

- (void)picPic*)pic DownloadFailWithErrorNSError *)error
{
    if(![pic isEqual:self.pic]){
        return;
    }
    [self.errorLabel setHidden:NO];
    [self.progressView setHidden:YES];
}

- (void)picPic*)pic DownloadDidProgress:(float)progress
{
    if(![pic isEqual:self.pic]){
        return;
    }
    [self.progressView setProgress:progress+.01f];
}

谢谢!



Best Answer-推荐答案


通过切换到 SDWebImage 3.0(而不是 3.3)显然解决了这个问题。 我会继续在项目 github 页面上声明一个问题,看看是否有人遇到了同样的问题。

关于ios - SDWebImage 线程占用 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18188452/

回复

使用道具 举报

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

本版积分规则

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