title: How to save a remote image with Swift?
body: <p>I'm trying to display and save images with Swift. On first hit, it shows the remote image on imageview, on second hit it shows blank imageview instead of it should be local image which saved on first hit.</p>
  
  <pre><code>    var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
      var imagePath = paths.stringByAppendingPathComponent("images/\(id)/logo.jpg" )
      var checkImage = NSFileManager.defaultManager()
  
      if (checkImage.fileExistsAtPath(imagePath)) {
          let getImage = UIImage(contentsOfFile: imagePath)
          self.image?.image = getImage
      } else {
          dispatch_async(dispatch_get_main_queue()) {
              let getImage =  UIImage(data: NSData(contentsOfURL: NSURL(string: remoteImage)))
              UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true)
              self.image?.image = getImage
          }
      }
  </code></pre>
  
  <p>Edit: This one worked for me.</p>
  
  <pre><code>var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
  var dirPath = paths.stringByAppendingPathComponent("images/\(id)" )
  var imagePath = paths.stringByAppendingPathComponent("images/\(id)/logo.jpg" )
  var checkImage = NSFileManager.defaultManager()
  
  if (checkImage.fileExistsAtPath(imagePath)) {
      let getImage = UIImage(contentsOfFile: imagePath)
      self.image?.image = getImage
  } else {
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
          checkImage.createDirectoryAtPath(dirPath, withIntermediateDirectories: true, attributes: nil, error: nil)
          let getImage =  UIImage(data: NSData(contentsOfURL: NSURL(string: remoteImage)))
          UIImageJPEGRepresentation(getImage, 100).writeToFile(imagePath, atomically: true)
  
          dispatch_async(dispatch_get_main_queue()) {
              self.image?.image = getImage
              return
          }
      }
  }
  </code></pre>