我想将图像发送到具有RestFul api的Web服务器。这就是我后面的内容。第一个RequestParam ecgImage是图像。
@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 {
......
}
我在ios中使用UNIRest libary来进行休息调用,我希望这样的东西能够正常工作
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) {
}];
如何发送图像?
在android中,我曾经将图像作为Bitmap,我就像这样发送它。
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));
但我现在在iOS中我只是一个UIImage。所以我试着搜索“将UIImage转换为ByteArrayBody”,但没有任何运气。什么是Objective-C中的等效代码
答案 0 :(得分:1)
您需要将其转换为NSData
对象:
NSData *_imageData = UIImageJPEGRepresentation(image, 1);
[parameters setObject:UIImageJPEGRepresentation(image, 1) forKey:@"ecgImage"];