Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

[SOLVED] Issue loading sprite from byte[] into UI Image component

Discussion in '2D' started by Connor_13_, May 29, 2015.

  1. Connor_13_


    Mar 14, 2014
    SOLVED: Changed mip mapping to false like this:
    Code (CSharp):
    1. Texture2D text = new Texture2D(1, 1, TextureFormat.ARGB32, false);
    Don't know why but it just works lol

    I created an application where I download an image, save it in PlayerPrefs as a Base64 String, and then load it into a UI Image component. I have had a success... to a degree.

    For some reason only the lower left had corner of my sprite will show when it is assigned to the UI Image component through my script, but the whole image shows if I add it to a SpriteRenderer through script.

    What UI Image should look like:

    What it actually looks like:

    This is the code that I use to convert the downloaded texture to a Base64 String and then assign it to the UI Image component.

    Code (CSharp):
    1. string imageStr = Convert.ToBase64String(download.bytes); //download is the WWW object
    2. PlayerPrefs.SetString("SecondaryImage_ByteString", imageStr);
    This is the code I use to create and apply the sprite.
    Code (CSharp):
    1. Texture2D text = new Texture2D(1, 1);
    2. text.LoadImage(Convert.FromBase64String(PlayerPrefs.GetString("PrimaryImage_ByteString")));
    4. Sprite sprite = Sprite.Create (text, new Rect(0,0,text.width,text.height), new Vector2(.5f,.5f));
    5. image.sprite = sprite; //Image is a defined reference to an image component
    If someone could help clue me in I would greatly appreciate it! (Sorry for the large images)
    JayGarxP and JohnTube like this.
  2. Ben-BearFish


    Sep 6, 2011
    @Connor_13_ You said you solved the issue. Do you mind posting the solution as it will help others and myself?
  3. Connor_13_


    Mar 14, 2014
    I believe I changed the Texture2D line to turn Mip Mapping to false. See the top line of my post under "Solved". Hopefully this helps.
  4. BytesCrafter


    Apr 16, 2016
    I used this method, you can also use cloud save just by saving the string to cloud like the google cloud.
    Hope this help! This is how I approach this kind of method, after all those reseach. :D

    You could try some of my sample apps here specially "Bytes Crafter - Unity Debug"
    It has a simple debug window what is currently on process in the script.

    public RawImage prevUserPhoto = ; This must be not null! Put a texture wd here!
    public RawImage newUserPhoto = ; Leave this null. This is where decoded texture go.

    >> ToLoad Image saved on PlayerPrefs as "string"

    Texture2D newPhoto = new Texture2D (1, 1);
    newPhoto.Apply ();
    newUserPhoto = newPhoto;

    >> ToSave Image from a Texture 2D which must be a "readable" and RGBA 32 format.

    string stringData = Convert.ToBase64String (prevUserPhoto .EncodeToJPG ()); //1
    PlayerPrefs.SetString(PhotoSaved, stringData );
    Mohsen-Animator likes this.