Search Unity

How to load a already saved png from an IOS path?

Discussion in 'Scripting' started by zyonneo, Mar 15, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I have already saved an image as a png file.The code for saving is given below

    Code (CSharp):
    1.  Texture2D tex2d = (Texture2D)textures;
    2.         var bytes = tex2d.EncodeToPNG();
    3.         //IN assets folder
    4.        // var dirPath = Application.dataPath + "/../SavaImages/";
    5.         var dirPath = Application.persistentDataPath;
    6.         if(!Directory.Exists(dirPath))
    7.         {
    8.             Directory.CreateDirectory(dirPath);
    9.            
    10.         }
    11.  
    12.         File.WriteAllBytes(dirPath + "img.png", bytes);
    Now I am trying to load the same png image into a Rawimage.When I test my app in unity editor it is working but when testing it in IOS device the raw image does not change its image.

    Code (CSharp):
    1. public RawImage img1;
    2. Texture2D tex1 = null;
    3.             byte[] fileData;
    4.  
    5.             //string filePath=  Application.dataPath + "/../SavaImages/img.png";
    6.             string filePath = Application.persistentDataPath+"img.png";
    7.  
    8.             if (File.Exists(filePath))
    9.             {
    10.                 fileData = File.ReadAllBytes(filePath);
    11.                 //tex = new Texture2D(2, 2);
    12.                 tex1 = new Texture2D(150, 150, TextureFormat.DXT1, false);
    13.                 tex1.LoadImage(fileData);   //..this will auto-resize the texture dimensions.
    14.                 img1.texture = tex1;
    15.             }
    16.  
    .I tried other path also but same result working in unity editor but not in IOS device
    string filePath= Application.dataPath + "/../SavaImages/img.png"
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You're setting the texture format to Dxt1, I think you need to use one supported by iOS.