菜鸟教程小白 发表于 2022-12-12 13:34:59

ios - SDWebImage 线程占用 CPU


                                            <p><p>这是我的问题:</p>

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

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

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

<p> <img src="/image/DHYQF.png" alt="hogging threads"/> </p>

<p>这是我的代码:</p>

<p><strong>UITableView 数据源</strong></p>

<pre><code>- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* cellIdentifier = [@&#34;HomePicCell&#34; stringByAppendingString:.currentTheme];
    HomePicCell* cell = ;

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

    if(self.pics.count&gt;0){
      Pic* pic = self.pics;
      ;
    }

    return cell;
}
</code></pre>

<p><strong>HomePicCell (configureWithPic:)</strong></p>

<pre><code>- (void)configureWithPic:(Pic*)pic
{
    self.pic = pic;

    // Reinit UI
    ;
    ;
    ;
    ;

    ;
}
</code></pre>

<p><strong>图片</strong></p>

<pre><code>- (void) downloadWithDelegate:(id&lt;PicDownloadDelegate&gt;)delegate
{
    SDWebImageManager *manager = ;

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

      if(expectedSize&gt;0){
            float progress = [@(receivedSize) floatValue]/[@(expectedSize) floatValue];
            ;
      }

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

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

      if(image){
            if(cacheType == SDImageCacheTypeNone){
                ;
            }else{
                ;
            }
      }else{
            ;
      }

    }];
}
</code></pre>

<p><strong>HomePicCell(委托(delegate)方法)</strong></p>

<pre><code>- (void)pic:(Pic*)pic DownloadDidFinish:(UIImage *)image fromCache:(BOOL)fromCache
{
    if(!){
      return;
    }

    ;
    self.photoImageView.image = image;
    ;
}

- (void)pic:(Pic*)pic DownloadFailWithError:(NSError *)error
{
    if(!){
      return;
    }
    ;
    ;
}

- (void)pic:(Pic*)pic DownloadDidProgress:(float)progress
{
    if(!){
      return;
    }
    ;
}
</code></pre>

<p>谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>通过切换到 SDWebImage 3.0(而不是 3.3)显然解决了这个问题。</strong>
我会继续在项目 github 页面上声明一个问题,看看是否有人遇到了同样的问题。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - SDWebImage 线程占用 CPU,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18188452/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18188452/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - SDWebImage 线程占用 CPU