【问题标题】:SDWebImage NSURLRequests failing intermittentlySDWebImage NSURLRequests 间歇性失败
【发布时间】:2013-04-24 20:47:27
【问题描述】:

我正在使用 SDWebImage 将图像从远程服务器加载到使用以下代码的 UICollectionView 中:

[myCell.imageView setImageWithURL:imgURL placeholderImage:nil options:SDWebImageRetryFailed success:^(UIImage *image)
 {
     [_imageCache storeImage:image forKey:[imgURL absoluteString] toDisk:YES];
 } failure:^(NSError *error){
     NSLog(@"ERROR: %@", error);
 }];

对于大多数单元格,此代码工作正常 - 它加载图像并将它们保存到我的本地磁盘。但是,在几张(似乎是随机的?)图像之后,它们会停止加载。然后我收到以下错误:

ERROR: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1d33fdc0 {NSErrorFailingURLStringKey=http://path/to/image.jpg, NSErrorFailingURLKey=http://path/to/image.jpg, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1d34c0f0 "The request timed out."}

发生这种情况时,我的应用似乎完全停止发送 NSURLRequests。一段时间后,大概 20-30 秒,我可以刷新表格,失败的图像将正确加载,应用程序将恢复对所有 NSURLRequests 的响应。

我发现如果我快速向下滚动我的收藏视图,这种情况往往会更频繁地发生。会不会尝试一次下载太多?有没有办法限制并发下载的数量?此方法在最新的 SDWebImage 代码中似乎已被弃用。

【问题讨论】:

    标签: ios xcode uicollectionview nsurlrequest sdwebimage


    【解决方案1】:

    想通了。我在我的应用程序的另一部分使用 MWPhotoBrowser。 MWPhotoBrowser 带有旧版/修改版的 SDWebImage。我从 Github 下载了最新版本的 SDWebImage,重命名/重构了所有文件,并将我新更新和修改的 SDWebImage 与 MWPhotoBrowser 所依赖的文件一起导入。

    新版SDWebImage彻底解决了我的问题!

    【讨论】:

      【解决方案2】:

      最好使用共享管理器异步下载,并在SDWebImage下载完成后显示。现在,您可以快速滚动进行测试。

      - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
      
          MyCell *myCell = (MyCell *)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
          //Set placeholder
          myCell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
      
          NSURL *cellImageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[self.collectionData objectAtIndex:indexPath.row]]];
          [myCell.cellIndicator startAnimating];
      
          SDWebImageManager *manager = [SDWebImageManager sharedManager];
          [manager downloadWithURL:cellImageURL
                          delegate:self
                           options:0
          success:^(UIImage *image, BOOL cached)
          { 
              //Display when finish download
              myCell.imageView.image = image;
              [myCell.cellIndicator stopAnimating];
          } failure:nil];
      
      
          return cell;
      
      }
      

      *编辑: 如果在设备上问题仍未解决:尝试分开下载和显示

      @interface ViewController ()
      @property (retain , nonatomic) NSMutableArray *linksArray;
      @property (retain , nonatomic) NSMutableArray *imagesArray;
      @end
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          [self initLinksArray];
      //Add placehoder.png to your imagesArray
          [self initImagesArray];
      //Download to NSMultableArray
          [self performSelectorInBackground:@selector(downloadImages) withObject:nil];
      
      }
      
      - (void)initImagesArray{
          self.imagesArray = [[[NSMutableArray alloc] init] autorelease];
          for (NSInteger i = 0; i < self.linksArray.count; i++) {
              [self.imagesArray addObject:[UIImage imageNamed:@"placeholder.png"]];
          }
      }
      
      - (void)downloadImages{
          for (NSInteger i = 0; i < self.linksArray.count; i++) {
              NSURL *cellImageURL = [NSURL URLWithString:[self.linksArray objectAtIndex:i]];
      
              SDWebImageManager *manager = [SDWebImageManager sharedManager];
              [manager downloadWithURL:cellImageURL
                              delegate:self
                               options:0
                               success:^(UIImage *image, BOOL cached)
               {
                   [self.imagesArray replaceObjectAtIndex:i withObject:image];
               } failure:nil];
          }
      }
      - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
      {
          return [self.imagesArray count];;
      }
      
      - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
          SDCell *cell = (SDCell *)[cv dequeueReusableCellWithReuseIdentifier:@"SDCell" forIndexPath:indexPath];
          cell.cellImageView.image = [self.imagesArray objectAtIndex:indexPath.row];
          return cell;
      }
      

      【讨论】:

      • 不幸的是,这种方法产生了同样的问题 :( 此外,这两种方法在 iOS 模拟器中都能完美运行 - 它只在出现问题的设备上。有没有办法在一次,然后是接下来的 10 次,等等?
      猜你喜欢
      • 2019-04-18
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多