title: UICollectionView scroll performance using AFNetworking to load images
body: <p>I have read quite a few of the UICollectionView posts about poor scrolling, but none seem to directly apply or they are still unanswered.</p>
  
  <p>I'm using <code>AFNetworking</code> to asynchronously load the images (95px squared) onto each cell and then when the images are scrolled into view again, the image is restored from cache (as verified by the response code given as 0 instead of 200).  </p>
  
  <p>Here's what I've tried:</p>
  
  <ul>
  <li>Commented out <code>weakCell.photoView.image = image;</code> so the images aren't draw on screen and the scrolling was smoother (still stuttered a little during the HTTP get)</li>
  <li>Removed all of the AFNetworking code from the <code>cellForRowAtIndexPath</code> method and the scrolling was much smoother (even with the custom cell shadows, etc. still being drawn on screen)</li>
  <li>When I draw only the cell view (with the shadows) on screen, scrolling is very smooth for 100 cells. As soon as I start drawing the images on screen, scrolling is very poor on my device and it's even noticeable on the simulator. Instagram has very smooth scrolling for hundreds of cells on their profile view, so I'm trying to get close to their performance.  </li>
  </ul>
  
  <p><strong>Are there any ways that I can improve any of my code below in order to improve scrolling performance?</strong> </p>
  
  <p>Here is my cell code:</p>
  
  <pre><code>#import "PhotoGalleryCell.h"
  
  @implementation PhotoGalleryCell
  
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self)
      {
          // Setup the background color, shadow, and border
          self.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f];
          self.layer.borderColor = [UIColor blackColor].CGColor;
          self.layer.borderWidth = 0.5f;
          self.layer.shadowColor = [UIColor blackColor].CGColor;
          self.layer.shadowRadius = 3.0f;
          self.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
          self.layer.shadowOpacity = 0.5f;
  
          // Make sure we rasterize for retina
          self.layer.rasterizationScale = [UIScreen mainScreen].scale;
          self.layer.shouldRasterize = YES;
  
          // Add to the content view
          self.photoView = [[UIImageView alloc] initWithFrame:self.bounds];
          [self.contentView addSubview:self.photoView];
      }
  
      return self;
  }
  
  - (void)prepareForReuse
  {
      [super prepareForReuse];
  
      self.photoView.image = nil;
      self.largeImageURL = nil;
  }
  </code></pre>
  
  <p>And here is my UICollectionView code:</p>
  
  <pre><code>#pragma mark - Collection View Delegates
  
  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  {
      return 1;
  }
  
  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  {
      return [zePhotos count];
  }
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      PhotoGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPGPhotoCellIdentifier forIndexPath:indexPath];
  
      // Get a reference to the image dictionary
      NSDictionary *photoDict = [[zePhotos objectAtIndex:indexPath.row] objectForKey:@"image"];
  
      // Asynchronously set the thumbnail view
      __weak PhotoGalleryCell *weakCell = cell;
      NSString *thumbnailURL = [[photoDict objectForKey:@"thumbnail"] objectForKey:@"url"];
      NSURLRequest *photoRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbnailURL]];
      [cell.photoView setImageWithURLRequest:photoRequest
                            placeholderImage:nil
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                         weakCell.photoView.image = image;
                                     }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                         NSLog(@"Error retrieving thumbnail... %@", [error localizedDescription]);
                                     }];
  
      // Cache the large image URL in case they tap on this cell later
      cell.largeImageURL = [[photoDict objectForKey:@"large"] objectForKey:@"url"];
  
      return cell;
  }
  
  - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  {
      [self performSegueWithIdentifier:@"showPhotoDetail" sender:self];
  }
  </code></pre>