title: UICollectionView lags when scroll
body: <p>I have a UICollectionView where i display cell that contain an png image each cell. When i display two er more cell beside each other the scroll will lag, but when i only display cells below eachother the scroll run more smotthly. </p>
  
  <p>In my cellForItemAtIndexPath i got this 2 lines of coe which was recommended in another stackoverflow thread: </p>
  
  <pre><code>    cell.layer.shouldRasterize = true;
      cell.layer.rasterizationScale = UIScreen.mainScreen().scale
  </code></pre>
  
  <p>It did help a little, but not much. </p>
  
  <p>The way i get the image is by making an extension to ImageView:</p>
  
  <pre><code>import Foundation
  import Alamofire
  
  let imageCache = NSCache()
  
  extension UIImageView {
      public func imageFromUrl(urlString: String) {
          if let image = imageCache.objectForKey(urlString) as? UIImage {
              self.image = nil
              self.image = image
          }
          else {
              self.image = nil
              Alamofire.request(.GET, urlString).responseJSON{
                  response in
                  if let tilbudimage = UIImage(data: response.data!){
                      self.image = tilbudimage
                      imageCache.setObject(tilbudimage, forKey: urlString)
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And in my cellForItemAtIndexPath i init the cell image like:</p>
  
  <pre><code>let cell  = collectionView.dequeueReusableCellWithReuseIdentifier("postcol", forIndexPath: indexPath) as! PostColViewCell
  cell.PostImg.imageFromUrl(firstImage["imgurl"].string!)
  </code></pre>
  
  <p>Display cells this way lag when scroll: </p>
  
  <p><a href="https://i.stack.imgur.com/1pGfR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1pGfR.png" alt="this lag"></a></p>
  
  <p>Display cells this way does not lag when scroll
  <a href="https://i.stack.imgur.com/paWGf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/paWGf.png" alt="enter image description here"></a></p>
  
  <p>Here is my whole cellForItemAtIndexPath:</p>
  
  <pre><code> func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
  
          let cell  = collectionView.dequeueReusableCellWithReuseIdentifier("postcol", forIndexPath: indexPath) as! PostColViewCell
  
          cell.layer.shouldRasterize = true;
          cell.layer.rasterizationScale = UIScreen.mainScreen().scale
  
          cell.layer.cornerRadius = 10
  
          let post = self.postArray[indexPath.row]
  
          let firstImage = post["images"].array![0]
  
          if(post["newpost"].int! &lt;= 1)
          {
              cell.NewLabel.hidden = false
          }
          else
          {
              cell.NewLabel.hidden = true
          }
  
          cell.ProfilePicture.imageFromUrl(post["user"]["photourl"].string!)
          cell.ProfilePicture.contentMode = .ScaleAspectFill
          cell.ProfilePicture.clipsToBounds = true
  
          cell.NameLabel.text = post["user"]["firstname"].string! + " " + post["user"]["lastname"].string!
  
          cell.PriceLabel.text = String(post["price"].int!) + " kr."
  
          cell.NameLabel?.userInteractionEnabled = true
          cell.NameLabel?.tag = indexPath.row
          let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "seeUser:")
          tapped.numberOfTapsRequired = 1
          cell.NameLabel?.addGestureRecognizer(tapped)
  
          cell.TitleText.textColor = UIColor.blackColor()
          cell.TitleText.labelSpacing = 5; // distance between start and end labels
          cell.TitleText.pauseInterval = 2.0; // seconds of pause before scrolling starts again
          cell.TitleText.scrollSpeed = 30; // pixels per second
          cell.TitleText.textAlignment = NSTextAlignment.Left; //centers text when no auto-scrolling is applied
          cell.TitleText.fadeLength = 12;// length of the left and right edge fade, 0 to disable
          cell.TitleText.font = UIFont(name: (cell.TitleText?.font.fontName)!, size: 12)!
          cell.TitleText.text = post["title"].string!
  
          if(cell.PostImg.image != nil)
          {
              cell.PostImg.imageFromUrl(firstImage["imgurl"].string!)
          }
  
          cell.PostImg.contentMode = .ScaleAspectFill
          cell.PostImg.clipsToBounds = true
  
          //Comment label layout
          cell.CommentLabel.text = String(post["comments"].array!.count)
          cell.CommentLabel.textColor = UIColor.whiteColor()
          // Here is the magic
          cell.CommentLabel.icon = UIImage(named: "Comment")// Set icon image
          cell.CommentLabel.icon = cell.CommentLabel.icon!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
          cell.CommentLabel.tintColor = UIColor.whiteColor()
          cell.CommentLabel.iconPadding = 5               // Set padding between icon and label
          cell.CommentLabel.iconPosition = SMIconLabelPosition.Left // Icon position
          cell.CommentLabel.textAlignment = .Right
  
  
          //Distance label layout
          if (post["distance"].double! &lt; 1)
          {
              cell.DistanceLabel.text = LoginViewController.CurrentUser.CurrentLocation
          }
          else
          {
              let formatter = NSNumberFormatter()
              formatter.minimumFractionDigits = 0
              formatter.maximumFractionDigits = 0
  
              cell.DistanceLabel.text = formatter.stringFromNumber(post["distance"].double!)! + " km"
          }
  
          cell.DistanceLabel.textColor = UIColor.whiteColor()
          // Here is the magic
          cell.DistanceLabel.icon = UIImage(named: "Location")// Set icon image
          cell.DistanceLabel.icon = cell.DistanceLabel.icon!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
          cell.DistanceLabel.tintColor = UIColor.whiteColor()
          cell.DistanceLabel.iconPadding = 0            // Set padding between icon and label
          cell.DistanceLabel.iconPosition = SMIconLabelPosition.Left // Icon position
          cell.DistanceLabel.textAlignment = .Left
  
          return cell
      }
  </code></pre>