Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func uploadImage(indicatorId: String, image: UIImage, onSuccess: (edges: String?) -> Void, onFailure: (error: NSError?) -> Void) {
- let url = Constants.workspaceBaseUrl+"/indicators/" + "\(indicatorId)/image"
- let headers = ["account": CacheManager.sharedInstance.getWorkspaceCredentials().workspaceAccount, "password": CacheManager.sharedInstance.getWorkspaceCredentials().workspaceAccountPassword]
- let apiObj = ApiStruct(url: url, type: .PUT, body: nil, headers: headers)
- if let imageData = UIImageJPEGRepresentation(image, 0.5) {
- // Calling...
- connectionObject.sendMultipartRequest(apiObj, data: imageData, onSuccess: { response in
- onSuccess(edges: response["data"].string)
- }, failure: { error in
- onFailure(error: error)
- })
- }
- }
- ========================
- // Called
- func sendMultipartRequest(apiObject: ApiStruct, data: NSData, onSuccess: (response: SwiftyJSON.JSON) -> Void, failure fail: (
- error: NSError?) -> Void) {
- UIApplication.sharedApplication().networkActivityIndicatorVisible = true
- var headers = apiObject.headers ?? [:]
- headers["Content-Type"] = "multipart/form-data"
- manager?.upload(apiObject.type, apiObject.url, multipartFormData: { (multipart) in
- multipart.appendBodyPart(data: data, name: "image", fileName: "image.jpg", mimeType: "image/jpg")
- }, encodingCompletion: { (encoding) in
- switch encoding {
- case .Success(let request, _, _):
- request.progress({ (one, two, three) in
- print("\(one) \(two) \(three)")
- })
- request.responseJSON(completionHandler: { (response) in
- if (response.result.isSuccess) {
- UIApplication.sharedApplication().networkActivityIndicatorVisible = false
- let data = JSON(response.result.value!)
- onSuccess(response: data)
- } else {
- UIApplication.sharedApplication().networkActivityIndicatorVisible = false
- fail(error: response.result.error)
- }
- })
- break
- case .Failure(let error):
- UIApplication.sharedApplication().networkActivityIndicatorVisible = false
- fail(error: error as NSError)
- break
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement