title: caching with alamofireimages
body: <p>I have a collection view that displays images, some of them are cached and properly retrieved, but others are not. 
  The collection view displays two squared cells per row.</p>
  
  <p>I cannot understand why sometimes the images are taken from the cache and other times have to be downloaded again.</p>
  
  <p>here where I cache the images:</p>
  
  <pre><code>class DataImage {
  
  static let sharedManager = DataImage()
  
  let decoder = ImageDecoder()
  let photoCache = AutoPurgingImageCache(
      memoryCapacity: 100 * 1024 * 1024,
      preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
  )
  
  
  //MARK: - Image Downloading
  
  func getNetworkImage(urlString: String, completion: (UIImage -&gt; Void)) -&gt; (ImageRequest) {
      let queue = decoder.queue.underlyingQueue
      let request = Alamofire.request(.GET, urlString)
      let imageRequest = ImageRequest(request: request)
      imageRequest.request.response(
          queue: queue,
          responseSerializer: Request.imageResponseSerializer(),
          completionHandler: { response in
              guard let image = response.result.value else {
                  return
              }
              let decodeOperation = self.decodeImage(image) { image in
                  completion(image)
                  self.cacheImage(image, urlString: urlString)
              }
              imageRequest.decodeOperation = decodeOperation
          }
      )
      return imageRequest
  }
  
  func decodeImage(image: UIImage, completion: (UIImage -&gt; Void)) -&gt; DecodeOperation {
      let decodeOperation = DecodeOperation(image: image, decoder: self.decoder, completion: completion)
      self.decoder.queue.addOperation(decodeOperation)
      return decodeOperation
  }
  
  //MARK: - Image Caching
  
  func cacheImage(image: Image, urlString: String) {
      photoCache.addImage(image, withIdentifier: urlString)
  }
  
  func cachedImage(urlString: String) -&gt; Image? {
      print("image: \(photoCache.imageWithIdentifier(urlString))")
      return photoCache.imageWithIdentifier(urlString)
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and here is the Cell class:</p>
  
  <pre><code>class StoreCell: UICollectionViewCell {
  
  @IBOutlet weak var title: UILabel!
  @IBOutlet weak var storyImage: UIImageView!
  @IBOutlet weak var subTitle: UILabel!
  @IBOutlet weak var cost: UILabel!
  
  var imageFilename: String!
  var request: ImageRequest!
  var dataImage = DataImage()
  
  
  func configure() {
      self.layer.cornerRadius = 3.0
      reset()
      loadImage()
  }
  
  func reset() {
      storyImage.image = nil
      request?.cancel()
  }
  
  func loadImage() {
  
      let stringURL = "https://........\(imageFilename)"
  
      if self.storyImage.image == nil {
          if let image = dataImage.cachedImage(stringURL) {
  
              self.storyImage.image = image
          } else {
              request = dataImage.getNetworkImage(stringURL) { image in
                  self.storyImage.image = image
                  self.storyImage.runImageTransition(.CrossDissolve(0.2), withImage: image)
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>