2

次のコードを使用して、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 コードでは非推奨になっているようです。

4

2 に答える 2

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;
}
于 2013-04-24T21:07:50.087 に答える