0

I want to send an image to web server which has RestFul api's. This is what I have in my backend. First RequestParam ecgImage is the image.

@RequestMapping(value = "/ecgimage", method = RequestMethod.POST)
    public void profileImage(@RequestParam("ecgImage") MultipartFile formData,
            @RequestParam("heartRate") int heartRate,
            @RequestParam("dateTime") String dateTime,
            @RequestParam("mac") String mac, @RequestParam("qrs") String qrs,
            @RequestParam("result") String result) throws Exception {
    ......
}

I'm using UNIRest libary in ios for making restful calls and I'm hoping something like this would work

UIImage *img = // this is the image that I want to send

NSDictionary* headers = @{@"Content-Type": @"application/x-www-form-urlencoded"};
NSMutableDictionary* parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[self.dateUtils convertNSDateToMMDDYYY:date] forKey:@"date"];
[parameters setObject:hr forKey:@"heartRate"];
[parameters setObject:hr forKey:@"result"];
[parameters setObject:qrs forKey:@"qrs"];
[parameters setObject:????????? forKey:@"ecgImage"];

[[UNIRest post:^(UNISimpleRequest *request) {
     [request setUrl:POstECGImage];
//   [request setHeaders:headers];
     [request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {

}];

How do I send the image?

In android I used to have the image as a Bitmap and I send it like this.

Bitmap bitmap = // contains the image

ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
            multipartEntity.addPart("ecgImage",
                    new ByteArrayBody(baos.toByteArray(), "ecg.jpg"));
            multipartEntity.addPart("dateTime", new StringBody(dateTime));
            multipartEntity.addPart("heartRate",
                    new StringBody(String.valueOf(heartRate)));
            multipartEntity.addPart("qrs", new StringBody(String.valueOf(qrs)));
            multipartEntity.addPart("result", new StringBody(result));
            multipartEntity.addPart("mac", new StringBody(mac));

But I now in iOS all I've is a UIImage. So i tried searching "convert UIImage to ByteArrayBody" but didn't have any luck. What would be equivalent code in Objective-C

Drunken Daddy
  • 7,326
  • 14
  • 70
  • 104
  • countered down votes. the question might be pretty obvious but at least this question is proper: shows relevant valid code, is more than a sentence, mentions what was searched... don't see why it'd be bad – Daij-Djan May 10 '16 at 12:36
  • I was also wondering the same thing. I searched a lot before posting this question, but didn't find anything useful – Drunken Daddy May 10 '16 at 15:56

1 Answers1

1

You need to convert it to an NSData object:

NSData *_imageData = UIImageJPEGRepresentation(image, 1);
[parameters setObject:UIImageJPEGRepresentation(image, 1) forKey:@"ecgImage"];
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • can you please tell me if this is the correct way to send a multipart request using UNIRest? In backend I'm getting this error... org.springframework.web.multipart.MultipartException: The current request is not a multipart request – Drunken Daddy May 12 '16 at 13:11