Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. func uploadImage(indicatorId: String, image: UIImage, onSuccess: (edges: String?) -> Void, onFailure: (error: NSError?) -> Void) {
  2.  
  3. let url = Constants.workspaceBaseUrl+"/indicators/" + "\(indicatorId)/image"
  4. let headers = ["account": CacheManager.sharedInstance.getWorkspaceCredentials().workspaceAccount, "password": CacheManager.sharedInstance.getWorkspaceCredentials().workspaceAccountPassword]
  5.  
  6. let apiObj = ApiStruct(url: url, type: .PUT, body: nil, headers: headers)
  7.  
  8. if let imageData = UIImageJPEGRepresentation(image, 0.5) {
  9.  
  10. // Calling...
  11. connectionObject.sendMultipartRequest(apiObj, data: imageData, onSuccess: { response in
  12.  
  13. onSuccess(edges: response["data"].string)
  14.  
  15. }, failure: { error in
  16.  
  17. onFailure(error: error)
  18. })
  19. }
  20. }
  21.  
  22.  
  23. ========================
  24.  
  25. // Called
  26. func sendMultipartRequest(apiObject: ApiStruct, data: NSData, onSuccess: (response: SwiftyJSON.JSON) -> Void, failure fail: (
  27. error: NSError?) -> Void) {
  28.  
  29. UIApplication.sharedApplication().networkActivityIndicatorVisible = true
  30.  
  31. var headers = apiObject.headers ?? [:]
  32.  
  33. headers["Content-Type"] = "multipart/form-data"
  34.  
  35. manager?.upload(apiObject.type, apiObject.url, multipartFormData: { (multipart) in
  36. multipart.appendBodyPart(data: data, name: "image", fileName: "image.jpg", mimeType: "image/jpg")
  37. }, encodingCompletion: { (encoding) in
  38. switch encoding {
  39. case .Success(let request, _, _):
  40. request.progress({ (one, two, three) in
  41. print("\(one) \(two) \(three)")
  42. })
  43. request.responseJSON(completionHandler: { (response) in
  44. if (response.result.isSuccess) {
  45.  
  46. UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  47. let data = JSON(response.result.value!)
  48. onSuccess(response: data)
  49.  
  50. } else {
  51.  
  52. UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  53. fail(error: response.result.error)
  54. }
  55. })
  56. break
  57. case .Failure(let error):
  58. UIApplication.sharedApplication().networkActivityIndicatorVisible = false
  59. fail(error: error as NSError)
  60. break
  61. }
  62. })
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement