- Home /
How to make a Photo Gallery?
Hi there. I'm triyng to make a photo gallery that loads photos from a local disk, makes previews, splits them between pages.. basic stuff. To create a full sized photo/preview I'm using the following code:
public Photo(string file_name, int width, int height, Vector2 position, string caption)
{
float time = Time.realtimeSinceStartup;
try {
texture = new Texture2D(2,2);
byte[] binaryImageData = File.ReadAllBytes(file_name);
Debug.Log ("loaded: " + (Time.realtimeSinceStartup - time).ToString());
texture.LoadImage(binaryImageData);
Debug.Log ("assigned: " + (Time.realtimeSinceStartup - time).ToString());
}
catch{
Debug.Log ("Null ref texture: " + file_name);
throw new Exception("Can't load texture:" + file_name);
}
float aspectRatio = texture.width / (float)texture.height;
int resizedWidth, resizedHeight;
if (texture.width / (float)width > texture.height / (float)height) { //Determine what dimension should be reduced more
resizedWidth = width;
resizedHeight = (int)(resizedWidth / aspectRatio);
} else {
resizedHeight = height;
resizedWidth = (int)(resizedHeight*aspectRatio);
}
texture = ScaleTexture (texture, resizedWidth, resizedHeight, TextureFormat.RGB24);
Debug.Log ("resized:"+ (Time.realtimeSinceStartup - time).ToString());
this.width = width;
this.height = height;
this.caption = caption;
this.position = position;
this.file_name = file_name;
}
The problem is: texture.LoadImage(binaryImageData) line takes way too long, like half a second (Photo is a JPEG file 4000x3000). This will lead to ridiculous loading/freezing times when dealing with hundreds of photos.
I tried to use WWW class and www.texture, www.textureNonReadable to no effect. Not only it almost doesn't reduce loading times (texture = www.texture line takes approx. 0.3 sec), but also has some known problems with unicode characters in the file path (which I have): WWW class doesn't read widechar url???
Please help and tell me, how can I load this many images? The only way I can think of for now, is to load them via multithreading. And I wish I can evade this.