SDWebImage NSURLRequests间歇性失败

时间:2013-04-24 20:47:28

标签: ios xcode uicollectionview nsurlrequest sdwebimage

我正在使用以下代码将使用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代码中已弃用。

2 个答案:

答案 0 :(得分:1)

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

新版SDWebImage完全解决了我的问题!

答案 1 :(得分: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;
}