title: caching images in collectionViewCell in Swift?
body: <p>I have images in my collectionViewCell's that are fetched and parsed via NSURLRequest, how do I cache these images so they don't have to start a new request with every single appearance/disappearance of the view?</p>
  
  <p>here is my code that fetches the images:</p>
  
  <pre><code>class funnyPicture: NSObject {
  
      var pfPicture : PFObject
      var coverImage : UIImage!
  
      init(pfPicture: PFObject) {
          self.pfPicture = pfPicture
      }
  
      func fetchCoverImage(completion: (image: UIImage?, error: NSError?) -&gt; Void) {
          let urlString = self.pfPicture["funnyPictures"] as! String
          let url = NSURL(string: urlString)
          let request = NSURLRequest(URL: url!)
          let queue = dispatch_get_main_queue()
  
          NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
              if error == nil {
                  self.coverImage = UIImage(data: data!)
                  completion(image: self.coverImage, error: nil)
              } else {
                  completion(image: nil, error: error)
              }
          }
      }
  }
  </code></pre>
  
  <p>and here is my collectionView code that parse the images to the collectionViewCell's:</p>
  
  <pre><code>   override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyCollectionViewCell
  
          // Configure the cell
          let book = self.books[indexPath.row]
          let coverImage = book.coverImage
          if coverImage == nil {
              book.fetchCoverImage({ (image, error) -&gt; Void in
                  if self.collectionView != nil {
                      collectionView.reloadItemsAtIndexPaths([indexPath])
                  }
              })
          } else {
              dispatch_async(dispatch_get_main_queue()){
                  let imageView = cell.imageView
                  imageView.image = book.coverImage
              }
          };
          if book.coverImage == nil {
              cell.imageView.userInteractionEnabled = false
              cell.userInteractionEnabled = false
          }else {
              cell.imageView.userInteractionEnabled = true
              cell.userInteractionEnabled = true
  
          }
  
          return cell
      }
  </code></pre>
  
  <p>While I've received references to third party frameworks, I haven't received any answer on how to implement them with the code I have provided in the question, or even an answer using apples already implemented caching mechanism.. The reason I put the code in the question was for use in an answer.. Thank you.</p>