Question

Without using any image loading library, I am loading and processing images like below:

ImageGenerator(){
  for(int i=0;i<100;i++){
      ImageProcess( data-i , i );
  }
}

ImageProcess( String data , int i ){
  final Image img = new Image(data);
  img.setVisible(false);
  RootPanel.get().add(img);
  GWT.log( "image " + i );
  img.addLoadHandler(new LoadHandler() {
      @Override
      public void onLoad(LoadEvent event) {
          GWT.log( "onload " + i + " " + img.getWidth() );
          ImageElement data = ImageElement.as(img.getElement());
          RootPanel.get().remove(img);
          obj.setData(data);
          ..obj processing
      }
  });
}

The result :

image 1;
image 2;
image 3;
..
image 98;
image 99; <-- finish for loop
onload 1; <-- start loading image
onload 2;
onload 3;
..
onload 98;
onload 99;

But I hope it looks like:

image 1;  <-- generate image and load image step by step
onload 1;
image 2;
onload 2;
image 3;
onload 3;
..
image 98;
onload 98;
image 99;
onload 99;

I know that after I assigned a value to img.src the image is not instantly available. It has to be loaded first. The img element will fire an onload event, as soon as the image is done loading. I try to add timer and put image to different DOM element. It doesn't work.

My questions are, How to trigger the image element to load immediately? or let ImageProcess wait until onload finish ?

Was it helpful?

Solution

 RootPanel.get().add(img);

With this line you force the image to load directly. I think your problem is, that the loop finished before even one image has been transferred from the server - therefor you see the image outputs first and then the onload outputs.

Now I am not sure why it is so important for you to add an image, perform its onload, add the next image, and so on...

But I am sure this can be easily achieved by writing a method that adds an image to the DOM, and in its onload handler you call the method again for the next image. And so on until you reach your stop criterion.

Greetings Stefan

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top