2016-08-26 9 views
0

私はSDWebImageライブラリとそのiPhoneで動作していますが、なぜこれがiPadで呼び出されないのか分かりません。それは私がcellForItemAtIndexPathにこの方法を置く。いずれかのブレークポイントにヒットしません。dispatch_async(dispatch_get_main_queue()はiPad上で動作していませんが、iPhoneで動作します)

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString* cellIdentifier = @"CustomCollectionViewCell"; 
    CustomCollectionViewCell* cell = (CustomCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
    [cell.downloadButton setTag:indexPath.row]; 
    [cell.downloadButton addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    catalogModel = [catalogArray objectAtIndex:indexPath.item]; 
    cell.cellDescription.text = catalogModel.catalogName; 
    SDWebImageManager *manager = [SDWebImageManager sharedManager]; 
    NSString *urlStr = catalogModel.imageNameThumbnail; 
    NSURL *url = [NSURL URLWithString:urlStr]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
      if ([self.catalogCollectionView.indexPathsForVisibleItems containsObject:indexPath]) 
      { 
        catalogModel = [catalogArray objectAtIndex:indexPath.item]; 
        [manager downloadImageWithURL:[NSURL URLWithString:catalogModel.imageNameThumbnail] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {}completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) 
        { 
         cell.cellImageView.image = image; 
         NSLog(@"%@",catalogModel.catalogName); 
        }]; 
      }; 

     }); 
    return cell; 
} 
+1

あなたのインデントはあなたが投稿したコードを理解することは不可能ですが、不可能にします。あなたは「私はこのメソッドをcellForItemAtIndexPathに入れます」と言っています。あなたが投稿したものは方法ではなく、コード断片です。あなたは 'cellForItemAtIndexPath'メソッド全体をポストし、ポストする前にコードを再インデントするためにconrol-Iを使うべきです。 –

+2

2つのネストされた呼び出しを使用してdispatch_asyncを実行し、外側のグローバルバックグラウンドキューと内側のメインキューをメインキューに移動します。すべてのことは、そのコードの呼び出しを遅らせることです。実際にこのコードで何をしようとしていますか? –

+0

また、 'downloadImageWithURL:options:progress:completed:'メソッドの完了ブロックがメインスレッドまたはバックグラウンドスレッドで呼び出されているかどうか知っていますか?それが問題になるバックグラウンドスレッドで呼び出された場合。 –

答えて

2

同時画像読み込みの実装に問題があります。-collectionView: cellForItemAtIndexPath:を呼び出している場合は、デバイスが仮定すると、メインキュー上でコードを実行しますその-downloadImageWithURL:options:progress:completed:メソッドは、バックグラウンドスレッドでイメージのロードを実行し、即座に返すので、dispatch_async(dispatch_get_main_queue()...のラッピングなしで呼び出すことができます。それ以外の場合は、完了ハッシュdlerは、メインスレッド上で実行されるため、コードが

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString* cellIdentifier = @"CustomCollectionViewCell"; 

    CustomCollectionViewCell* cell = (CustomCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    ... 

    SDWebImageManager *manager = [SDWebImageManager sharedManager]; 

    // methods with completion block usually return instantly 
    [manager downloadImageWithURL:aURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize){} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 
     // ensure ui is updating on main thread and for visible cell 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if ([collectionView.indexPathsForVisibleItems containsObject:indexPath]) { 
       cell.cellImageView.image = image; 
      } 
     }); 
    }]; 

    return cell; 
} 

次のようになります。また、iPhoneやiPad上の異なる結果が試験装置の技術仕様のアーキテクチャの違いによって説明することができます。

+0

良い説明。そうでないならば、OPは、「これはiPadでは呼び出されていない」という意味ではっきりする必要があります。 –

+0

ありがとうございます。それは魅力のように働いた –

関連する問題