title: How to upload images to a server in iOS with Swift?
body: <p>It won't work, how to send an image from an iOS Swift app to my PHP server?</p>
  
  <pre><code>@IBAction func upload(sender: UIButton) {
  
      var imageData = UIImageJPEGRepresentation(img.image, 90)
     // println(imageData)
      let url = NSURL(string:"http://www.i35.club.tw/old_tree/test/uplo.php")
      //let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
      //var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 10)
      var request = NSMutableURLRequest(URL: url)
      request.HTTPMethod = "POST"
  
      // set Content-Type in HTTP header
      let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
      let contentType = "multipart/form-data; boundary=" + boundaryConstant
      NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
      request.HTTPBody = imageData
      // set data
      //var dataString = "adkjlkajfdadf"
      //let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
      //request.HTTPBody = requestBodyData
  
  
      //
  
      request.addValue(contentType, forHTTPHeaderField: "Content-Type")
      request.addValue("multipart/form-data", forHTTPHeaderField: "Accept")
      //
      // set content length
  
      //NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)
  
      var response: NSURLResponse? = nil
      var error: NSError? = nil
      let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&amp;response, error:&amp;error)
  
      let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
      println("API Response: \(results)")
  }
  
  //take photo
  func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
  
  
      var selectedImage : UIImage = image
       img.image = selectedImage
      UIImageWriteToSavedPhotosAlbum(selectedImage, nil, nil, nil)
      self.dismissViewControllerAnimated(true, completion: nil)
  
  }
  </code></pre>