title: Swift - How to save memory using AlamofireImage/ImageShack?
body: <p>I'm using <strong>Alamofire</strong>, <strong>AlamofireImage</strong> to cache images using <strong>ImageShack</strong> storage with <strong>Firebase</strong> backend. Sometimes memory comes over 100MB. I want to learn how to improve my image caching and well scaling before uploading. For example I have declared one common cache in class whole project using that <strong>AutoPurgingImageCache</strong>:</p>
  
  <pre><code>let photoCache = AutoPurgingImageCache(
          memoryCapacity: 100 * 1024 * 1024,
          preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
      )
  </code></pre>
  
  <p>Is there any way to define these <strong>memoryCapacity</strong>, <strong>preferredMemoryUsageAfterPurge</strong> to save memory? What do they mean exactly?</p>
  
  <p>I'm using below <strong>Alamofire.upload</strong> for <strong>didFinishPickingImage</strong>:</p>
  
  <pre><code>func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
  
          let urlStr = "http://post.imageshack.us/upload_api.php"
          let url = NSURL(string: urlStr)!
          let imgData = UIImageJPEGRepresentation(image, 0.25)!
  
          let keyData = "____IMGSHACK_API_KEY_____".dataUsingEncoding(NSUTF8StringEncoding)!
          let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!
          let publicData = "no".dataUsingEncoding(NSUTF8StringEncoding)!
  
          Alamofire.upload(.POST, url, headers: nil,
                           multipartFormData: { multipartFormData in
                              multipartFormData.appendBodyPart(data: imgData, name:"fileupload", fileName:"image", mimeType: "image/jpg")
                              multipartFormData.appendBodyPart(data: keyData, name: "key")
                              multipartFormData.appendBodyPart(data: keyJSON, name: "format")
                              multipartFormData.appendBodyPart(data: publicData, name: "public")
              }, encodingCompletion: { encodingResult in
                  switch encodingResult {
                  case .Success(let upload, _, _):
                      upload.validate()
                      upload.responseJSON { response in
                          let responseJSON = response.result.value as? [String: AnyObject]
                          if let links = responseJSON!["links"] as? Dictionary&lt;String, AnyObject&gt; {
                              if let imgLink = links["image_link"] as? String {
                                  self.postToFirebase(imgLink)
                                  print(imgLink)
                              }
                          }
                      }
                  case .Failure(let encodingError):
                      print(encodingError)
                  }
          })
  
          self.dismissViewControllerAnimated(true, completion: nil)
          self.tableView.contentOffset = CGPointMake(0, -60);
  
  }
  </code></pre>
  
  <p>I think <strong>UIImageJPEGRepresentation(image, 0.25)!</strong> is not well compression case, my images comes from internet as full size. If I scale before compression to upload like:</p>
  
  <pre><code>let size = CGSize(width: 500.0, height: 273.0)
  let scaledImage = image.af_imageScaledToSize(size)
  </code></pre>
  
  <p>I think web platform can be bad image quality. So I do scale and caching after image downloading:</p>
  
  <pre><code>    //MARK: - Image Downloading
  
      func getNetworkImage(urlString: String, comesFrom: String, completion: (UIImage -&gt; Void)) -&gt; (Request) {
  
          return Alamofire.request(.GET, urlString).responseImage { (response) -&gt; Void in
              guard let image = response.result.value else { return }
              completion(image)
  
              if comesFrom == "collectionPage" {
                  self.cacheCollectionImage(image, urlString: urlString)
              }
              else if comesFrom == "postPage" {
                  self.cachePostImage(image, urlString: urlString)
              }
  
          }
      }
  
      //MARK: - Image Caching
  
      func cacheCollectionImage(image: Image, urlString: String) {
  
          let size = CGSize(width: 188.0, height: 120.0)
          let scaledImage = image.af_imageScaledToSize(size)
  
          photoCache.addImage(scaledImage, withIdentifier: urlString)
  
      }
  
      func cachePostImage(image: Image, urlString: String) {
  
          let size = CGSize(width: 300.0, height: 165.0)
          let scaledImage = image.af_imageScaledToSize(size)
  
          photoCache.addImage(scaledImage, withIdentifier: urlString)
  
      }
  
  
      func cachedImage(urlString: String) -&gt; Image? {
          print("cachedImage() func ended")
          return photoCache.imageWithIdentifier(urlString)
      }
  </code></pre>
  
  <p>Also my application <strong>opens with 40MB</strong> memory usage. And than I see max <strong>top usage 130MB</strong>, after uploads and scrolling to turn back <strong>AutoPurgingImageCache</strong> works and decreasing memory using but I think still so much.</p>
  
  <p><strong>What do you advice me</strong> to save memory in these conditions? I need some explanations, what you want to say tools or strategy to handle it as possible as good quality for every platforms.</p>