title: Poor UICollectionView Scrolling Performance With UIImage
body: <p>I have a UICollectionView in my app, and each cell is a UIImageView and some text labels. The problem is that when I have the UIImageViews displaying their images, the scrolling performance is terrible. It's nowhere near as smooth as the scrolling experience of a UITableView or even the same UICollectionView without the UIImageView.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/13864958/how-to-get-a-snappy-uicollectionview-with-lots-of-images-50-200" title="this question">this question</a> from a few months ago, and it seems like an answer was found, but it's written in RubyMotion, and I don't understand that. I tried to see how to convert it to Xcode, but since I have never used NSCache either, it's a little hard to. The poster there also pointed to <a href="https://stackoverflow.com/questions/10400209/custom-uitableviewcell-with-large-uiimageview-causing-scroll-lag?answertab=oldest#tab-top">here</a> about implementing something in addition to their solution, but I'm not sure where to put that code either. Possibly because I don't understand the code from the <a href="https://stackoverflow.com/questions/13864958/how-to-get-a-snappy-uicollectionview-with-lots-of-images-50-200" title="this question">first question</a>.</p>
  
  <p>Would someone be able to help translate this into Xcode?</p>
  
  <pre><code>def viewDidLoad
    ...
    @images_cache = NSCache.alloc.init
    @image_loading_queue = NSOperationQueue.alloc.init
    @image_loading_queue.maxConcurrentOperationCount = 3
    ...
  end
  
  def collectionView(collection_view, cellForItemAtIndexPath: index_path)
    cell = collection_view.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: index_path)
    image_path = @image_paths[index_path.row]
  
    if cached_image = @images_cache.objectForKey(image_path)
      cell.image = cached_image
    else
      @operation = NSBlockOperation.blockOperationWithBlock lambda {
        @image = UIImage.imageWithContentsOfFile(image_path)
        Dispatch::Queue.main.async do
          return unless collectionView.indexPathsForVisibleItems.containsObject(index_path)
          @images_cache.setObject(@image, forKey: image_path)
          cell = collectionView.cellForItemAtIndexPath(index_path)
          cell.image = @image
        end
      }
      @image_loading_queue.addOperation(@operation)
    end
  end
  </code></pre>
  
  <p>Here is the code from the <a href="https://stackoverflow.com/questions/10400209/custom-uitableviewcell-with-large-uiimageview-causing-scroll-lag?answertab=oldest#tab-top">second question</a> that the asker of the <a href="https://stackoverflow.com/questions/13864958/how-to-get-a-snappy-uicollectionview-with-lots-of-images-50-200" title="this question">first question</a> said solved the problem:</p>
  
  <pre><code>UIImage *productImage = [[UIImage alloc] initWithContentsOfFile:path];
  
  CGSize imageSize = productImage.size;
  UIGraphicsBeginImageContext(imageSize);
  [productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
  productImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  </code></pre>
  
  <p>Again, I'm not sure how/where to implement that.</p>
  
  <p>Many thanks.</p>