菜鸟教程小白 发表于 2022-12-11 19:37:35

ios - 从 URL 和 Cache 中一张一张地加载和显示图片


                                            <p><p>我正在使用 SDWebImage 库在带有页面控件的 ScrollView 中显示图像。</p>

<p>如何一张一张下载图片并显示。假设我有 4 个 URL:url1,url2,url3,url4
所以我想先显示 url1 然后...2,3,4..等等...2,3,4 应该在后台加载,直到不下载占位符图像显示在那里。</p>

<p>下面是我尝试的内部 for 循环代码,它完美吗?</p>

<pre><code>      NSString* serverImgicon=&lt;url here in a loop&gt;;
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
            NSURL *url=;
            //set your image on main thread.
            dispatch_async(dispatch_get_main_queue(),^{
                [img sd_setImageWithURL:url
                     placeholderImage:];
            });
      });
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试下面的代码,它会正常工作的</p>

<pre><code> -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

      CollectionViewCell *cell =;

      myobject *newobject = _myarray;
      cell.nameLabel.text =newobject.name;

          dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
      dispatch_async(queue, ^{

            ;
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                                   ]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                if(image!= nil){
                  [ setImage:image];
                  ;

                }else{
                  UIImage * image = ;
                  [ setImage:image];
                  ;
                  ;

                }


                ;
            });
      });

      return cell;
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 URL 和 Cache 中一张一张地加载和显示图片,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43840475/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43840475/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 URL 和 Cache 中一张一张地加载和显示图片