• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Captain_Bravo · Jan 02, 2020 at 12:42 PM · iostexturetexture2dencodetopngput

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

185 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges