2013-04-24 56 views
2

我使用SDWebImage成UICollectionView使用下面的代码在远程服务器加载图像:SDWebImage NSURLRequests没有间歇

[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秒)后,我可以刷新表格,并且失败的图像将正确加载,应用程序将恢复对所有NSURLRequest的响应。

我发现这往往会更频繁地发生,如果我快速向下滚动我的收藏视图。它可能试图一次下载太多吗?有没有办法限制并发下载的数量?此方法在最新的SDWebImage代码中似乎不推荐使用。

回答

1

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

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

0

如果使用共享管理器进行异步下载,并在使用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; 
} 
+0

不幸的是,这种方法会产生同样的问题:(另外,这两种方法都可以在iOS模拟器中完美工作 - 它只存在于出现问题的设备上。是否有一种方法可以一次仅加载10个图像,然后下一个10,etc? – 2013-04-24 21:58:33

+0

看到我的编辑答案! – strong 2013-04-24 23:12:57

+0

实施您编辑的答案,并得到相同的错误 – 2013-04-25 15:53:42