Advice on loading multiple images in thread + Image loading issue from URL

Hi,

I am facing issue in loading image from web url stream.

String imgUrlStr = "https://live.staticflickr.com/65535/49881562182_a68f433eb2_m.jpg";
URL imgURL(imgUrlStr);
InputStream* input = imgURL.createInputStream(false);
Image im = ImageFileFormat::loadFrom(*input);
currentImageComponent->setImage(im);

One issue I faced was that earlier I was passing true in createInputStream which uses post method to get data that does not get authorized and return 403 code.

Now when i pass false and use get method to create inputStream i am getting response code 200 but when try to load the image using load from it does not create Image, in small debug i could see is findImageFormatForStream fails to find the format of the image from the stream.

If I use that image to check on browser(postman app as well) it works fine.

Also, i need some advice that I need to load such 1000 images from vector in 1000 ImageComponenet, what is advisable to do it smoothly ?how can I use threading to make UI smooth in this case.

Found that following also state the same issue.

ImageReadFromURL

Still if anyone could help in solving thread related, it would be good help.

The InputStream returned is a WebInputStream, which is not seekable (at least not backwards).

WebInputStream.setPosition():

[…] For a WebInputStream, this method will fail if wantedPos is smaller than the current position. If wantedPos is greater than the current position, then calling setPosition is the same as calling read, i.e. the skipped data will still be downloaded, although skipped bytes will be discarded immediately.

To probe the image formats the different ImageFormat implementations will start to read, and if they fail, they try to seek the stream back to zero. Since that does not have an effect on a WebInputStream, the next ImageFormat will fail in any case.

The solution is to read into a file or MemoryBlock and open the image from that.

EDIT: followed the link, seems there is the exact same advise. sorry for double posting then. But that means it should be accurate, if two people came to the same conclusion independently :wink:

Correct reading from MemoryBlock works fine.
Any info regarding thread problem ? how to efficiently read 1000 URL images and showing in ImageComponent.

Thanks in advance.

I would use a ThreadPool/ThreadPoolJob, so you can define the number of parallel executions but conveniently queue all your requests.

1 Like