- Home /
Bug - Texture "corrupted" after sending on server with MultipartformDataSection, only on iOS
Hello, I am working on an application using the camera and gallery functions, and I need to put the texture on API with form-data. So I am encoding the sprite I get from gallery with Texture2D.EncodeToPNG() to obtain a byte[] format. Then I send it on server with a Multipart form with a PUT method, like this :
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormFileSection("photo", myPhoto, "photo.png", "image/png"));
byte[] boundary = UnityWebRequest.GenerateBoundary();
string boundaryText = Encoding.UTF8.GetString(boundary);
byte[] formSections = UnityWebRequest.SerializeFormSections(formData, boundary);
int formSectionsCount = formSections.Length;
byte[] terminate = Encoding.UTF8.GetBytes("\r\n--" + boundaryText + "--");
int terminateCount = terminate.Length;
byte[] body = new byte[formSectionsCount + terminateCount];
Buffer.BlockCopy(formSections, 0, body, 0, formSectionsCount);
Buffer.BlockCopy(terminate, 0, body, formSectionsCount, terminateCount);
UnityWebRequest request = UnityWebRequest.Put(myUrl, (byte[])null);
request.SetRequestHeader("Authorization", authorization);
UploadHandler uploader = new UploadHandlerRaw(body);
uploader.contentType = "multipart/form-data; boundary=" + boundaryText;
request.uploadHandler = uploader;
request.chunkedTransfer = false;
using (request)
{
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
Debug.Log(request.responseCode);
Debug.Log(request.downloadHandler.text);
}
else
{
// Here is my positive calling coroutine
}
}
Everything is working great on PC and Android, but when I do a build on iOS, the data I get on the server is said to be "not an image or corrupted". I verified the texture just before the call and it has no problem on it, can even use the data to create a new texture successfully.
I am working on Unity 2019.1.4f1. I also have two plugins, Native Gallery (https://github.com/yasirkula/UnityNativeGallery) and Native Camera (https://github.com/yasirkula/UnityNativeCamera), and the Facebook SDK in the app.
Thanks for your help.
Answer by Captain_Bravo · Jan 15, 2020 at 02:21 PM
Solved the problem by changing EncodeToPNG() by EncodeToJPG(), same for the form data ("image/jpg"). Still curious of a way to use EncodeToPNG() properly though.
Your answer
Follow this Question
Related Questions
Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers
How do I prevent Texture2D.EncodeToPNG from creating artifacts on iOS? 1 Answer
EncodeToPng that works more smoothly on mobile 1 Answer
Texture2D.EncodeToPNG not working on iOs on Unity 3.5.5f3 1 Answer
EncodetoPNG and all other file types leaves the image in the wrong color space 0 Answers