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

标题: ios - SDWebImage 线程占用 CPU [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:34
标题: ios - SDWebImage 线程占用 CPU

这是我的问题:

我有一个包含自定义 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/






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