Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity runtime error ios. NullReferenceException: Unable to load image

Discussion in 'iOS and tvOS' started by soumya76, Dec 9, 2018.

  1. soumya76

    soumya76

    Joined:
    Dec 29, 2017
    Posts:
    2
    Hi Guys,

    Not getting this problem on the editor or android, but I am getting an error while running my unity app on ios, when I take a screenshot and save it to a file which I call "testpicture12.png", and try to read the image from that filepath from the Image constructor, I am getting this error.

    NullReferenceException: Unable to load image from file "/var/mobile/Containers/Data/Application/2E530180-5A74-4CE4-AE86-C75074D9491D/Documents/testpicture12.png".

    Here is my code:

    string path = Application.persistentDataPath + "/testpicture12.png";
    FileStream file;
    file = File.Open(path,FileMode.Create);
    if (file == null) print("file is null"); else print("file not null");
    var binary = new BinaryWriter(file);

    print(Screen.width + ", "+ Screen.height);
    Texture2D screenImage = new Texture2D(600, 360);
    //Get Image from screen
    screenImage.ReadPixels(new Rect(347, 153, 600, 360), 0, 0);
    screenImage.Apply();

    print("ReadPixels");

    UnityEngine.Color[] pixels = screenImage.GetPixels();
    if (screenImage == null) { print("NULL"); }else { print("NOT NULL"); }
    byte[] bytes = screenImage.EncodeToPNG();
    binary.Write(bytes);
    file.Close();

    picture19 = new Image<Bgr, byte>(path);

    Please help if you have any clue, any help would be greatly appreciated! I have been stuck on this problem for Days.