У меня есть изображение, которое я пытаюсь отправить в приложение rails через мое приложение IOS.Отправить изображение IOS через api
Шаг 1:
Это захватывает изображение из 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!)
}
Шаг 2:
Это услуга
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] as Character == "S") {
notifier.text = "This should be success"
} else {
notifier.text = "failure"
}
}
}
В этом код в настоящее время вызывает ошибку: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)'
Как упаковать изображение для отправки через API?
Примечание:
Я считаю, что проблема исходит от этой линии именно:
self.post("image/build", data: dataDictionary).responseJSON { (response) -> Void in
поста определяется как:
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())
}