Unity IOS NullReferenceException: Unable to load image

Post your iOS related question here.
Post Reply
Soumya76
Posts: 4
Joined: Tue Jan 02, 2018 4:24 pm

Unity IOS NullReferenceException: Unable to load image

Post by Soumya76 » Sat Dec 08, 2018 6:24 pm

Hi Guys,

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, I Will Greatly Appreciate any help! I have been stuck on this problem for Days!

Soumya76
Posts: 4
Joined: Tue Jan 02, 2018 4:24 pm

Re: Unity IOS NullReferenceException: Unable to load image

Post by Soumya76 » Thu Dec 20, 2018 4:57 pm

Hi,
Would really like a response to this question. Really think there is a problem with the Image Constructor given by EMGUCV when building for ios through unity, if not please provide me some insight.

Thanks,

canming
Site Admin
Posts: 1011
Joined: Thu Feb 28, 2008 4:16 pm

Re: Unity IOS NullReferenceException: Unable to load image

Post by canming » Tue Jan 08, 2019 5:06 pm

The Open CV library use a different version of libpng compares with the one used in Unity. If we build Open CV for iOS with PNG, it will conflict with the Unity PNG library and will results in a compilation error. Therefore the Open CV library was build without PNG library for iOS unity release. If you are using Unity and need to read a PNG file, please use Unity to read it into a Texture2d and then convert it to Emgu CV Mat file. In the next release, we will try to make it more transparent:
e.g. in unity iOS, when calling CvInvoke.Imread, we will try to load the image file using Unity instead of Open CV

Post Reply