Last active
April 13, 2016 21:38
-
-
Save Edudjr/f9e15f46c4606a8336babf9212748419 to your computer and use it in GitHub Desktop.
This is an example of how to use Alamofire to upload an image to a server.
This file contains 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 Foundation | |
import Alamofire | |
import ModelRocket | |
class ImageUploader { | |
static func uploadImage(image : UIImage, | |
parameters: [String: String]? = nil, | |
completionSuccess: (result: JSON) -> Void, | |
completionError: (error: NSError) -> Void) { | |
let imageData = UIImageJPEGRepresentation(image, 0.5) | |
Alamofire.upload( | |
.POST, | |
"http://URLHERE", | |
headers: ["Authorization" : "Basic"], | |
multipartFormData: { multipartFormData in | |
multipartFormData.appendBodyPart(data: imageData!, name: "imagefile", | |
fileName: "image.jpg", mimeType: "image/jpeg") | |
}, | |
encodingCompletion: { encodingResult in | |
switch encodingResult { | |
case .Success(let upload, _, _): | |
upload.validate() | |
upload.response{ request, response, data, error in | |
if (error != nil) { | |
completionError(error: error!) | |
}else{ | |
completionSuccess(result:JSON(data: data!)) | |
} | |
} | |
case .Failure(let encodingError): | |
completionError(error: encodingError as NSError) | |
} | |
}) | |
} | |
} | |
//Usage: | |
// .... | |
// let image = self.imageView.image | |
// WebServiceManager.uploadImage(image, completionSuccess: { (result) in | |
// //Success | |
// }) { (error) in | |
// //Error | |
// } | |
// .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment