【问题标题】:ios, convert UIImage to a MultipartFileios,将 UIImage 转换为 MultipartFile
【发布时间】:2016-05-10 11:28:48
【问题描述】:

我想将图像发送到具有 RestFul api 的网络服务器。这就是我的后端。第一个 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 库来进行安静的调用,我希望这样的东西可以工作

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 = // 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 中的等效代码是什么

【问题讨论】:

  • 反对投票。这个问题可能很明显,但至少这个问题是正确的:显示相关的有效代码,不仅仅是一个句子,提到搜索的内容......不明白为什么会不好
  • 我也想知道同样的事情。在发布这个问题之前我搜索了很多,但没有找到任何有用的东西

标签: ios objective-c uiimage multipart unirest


【解决方案1】:

您需要将其转换为NSData 对象:

NSData *_imageData = UIImageJPEGRepresentation(image, 1);
[parameters setObject:UIImageJPEGRepresentation(image, 1) forKey:@"ecgImage"];

【讨论】:

  • 你能告诉我这是否是使用 UNIRest 发送多部分请求的正确方法吗?在后端我收到此错误... org.springframework.web.multipart.MultipartException:当前请求不是多部分请求
猜你喜欢
  • 2020-05-07
  • 2018-12-10
  • 2013-08-10
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多