【发布时间】:2017-10-11 06:53:55
【问题描述】:
我正在尝试实现一个用于上传照片的 api。输入 json 为{"Photo":"base64stringValue"}。
用于将 uiimage 转换为 base64string 的代码是
let imageData = UIImageJPEGRepresentation(image, 0.9)
let imageStr = imageData?.base64EncodedString(options: .lineLength64Characters)
转换成功。当我将获得的imageStr 转换为图像时,我能够获得正确的图像。但是,在将此字符串传递给 {"Photo":imageStr} 时,我收到一条错误消息
SUCCESS: {
Message = "The request is invalid.";
ModelState = {
model = (
"An error has occurred."
);
};
在检查时,我发现 base64string 现在包含插入其间的随机 \r\ 值。这是一个问题吗?如果是,如何解决?
【问题讨论】:
-
删除
.lineLength64Characters选项? – 来自文档:“将最大行长度设置为 64 个字符,然后插入行尾。” -
JSON 字符串中不能有回车或换行。您如何实际创建 JSON 字符串以及如何将其转换回 Base64 字符串?