Created
April 6, 2017 06:36
-
-
Save hassanabidpk/f984e079a3ac2741e83b7f5f46e2f5b9 to your computer and use it in GitHub Desktop.
For medium blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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