swift - Send an IOS image across an api -
i have image trying send rails app via ios app.
step 1:
this grabs image uiview
@iboutlet var notifier: uilabel! @iboutlet var image: uiimage @ibaction func build(sender: anyobject) { let image_data = uiimagepngrepresentation(image.image!) self.service.createnewimage(notifier: notifier, image: image_data!) }
step 2:
this service
func createnewimage(notifier: uilabel, image: nsdata) { let datadictionary = ["image_data": image] self.post("image/build", data: datadictionary).responsejson { (response) -> void in if(response.description[response.description.startindex] character == "s") { notifier.text = "this should success" } else { notifier.text = "failure" } } }
this code causes error: *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'invalid type in json write (nsconcretemutabledata)'
how package image send across api?
notes:
i believe issue coming line specifically:
self.post("image/build", data: datadictionary).responsejson { (response) -> void in
post defined as:
func post(path: string) -> request { return self.post(path, data: nsdictionary()) } func post(path: string, data: nsdictionary) -> request { let url = self.url_from_path(path) return self.client.request(.post, url, parameters: data as? [string : anyobject] , encoding: parameterencoding.json, headers: self.getheaders()) } func patch(path: string, data: nsdictionary) -> request { let url = self.url_from_path(path) return self.client.request(.patch, url, parameters: data as? [string : anyobject] , encoding: parameterencoding.json, headers: self.getheaders()) }
this works:
let strbase64:string = image.base64encodedstringwithoptions(.encoding64characterlinelength) let datadictionary = ["image_data": strbase64]
you convert nsdata cannot put json base64 string can put json
Comments
Post a Comment