Skip to content

Instantly share code, notes, and snippets.

@hassanabidpk
Created April 6, 2017 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hassanabidpk/f984e079a3ac2741e83b7f5f46e2f5b9 to your computer and use it in GitHub Desktop.
Save hassanabidpk/f984e079a3ac2741e83b7f5f46e2f5b9 to your computer and use it in GitHub Desktop.
For medium blog
import UIKit
import Alamofire
class NewRestaurantViewController: UIViewController {
static let REST_UPLOAD_API_URL = "YOUR_POST_REQUEST_URL"
override func viewDidLoad() {
super.viewDidLoad()
// If you have any autorization headers
let authToken = "Token \(user_token!)"
let headers = [
"Authorization": authToken
]
let parameters: Parameters = ["name": "test_place",
"description": "testing image upload from swift"]
Alamofire.upload(
multipartFormData: { multipartFormData in
if let image = self.imageView.image {
let imageData = UIImageJPEGRepresentation(image1, 0.8)
multipartFormData.append(imageData!, withName: "image", fileName: "photo.jpg", mimeType: "jpg/png")
}
for (key, value) in parameters {
if value is String || value is Int {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
}
},
to: REST_UPLOAD_API_URL,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print("encoding Error : \(encodingError)")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment