11 Replies Latest reply on May 12, 2009 3:06 AM by Starlover_jacob

    multiple loaders, one complete event

    Starlover_jacob Level 1

      Hi,

       

      What i have:

      I have a buch of data stored in a sqlite db.

      Getting that data is no problem.

      With that data i get 2 image paths to locally stored images.

       

      Now i want to send the data to a server using a remoteObject and amfphp.

      Getting to the server and sending plain text is no problem.

       

      Now my images.. what i need to do is get the url of my image, load the image and convert it to bitmapdata, so i can send it to the server.

      On the server I let php take care of the bitmapdata and save the image.

      This way i know when the total saving of one item is done, and i can send a message back to flex.

       

      You may ask.. why don't you let flex take care of the saving on the server. Well, if i do that, i have seperate saving of the text data and the image. If the image is saved, the text data needs to be saved, so the path to that image is correct.

       

      Anyways,

      Is it possible to load 2 images using loader, and have one complete event?

      Or is there a better solution to get 2 bitmapdata's of the images.. so i can send all data of one item at once.

       

      (item contains name, infotext, fullimgurl, thumbimgurl)

       

      Please some help.

       

      Greets, Jacob

        • 1. Re: multiple loaders, one complete event
          Barna Biro Level 3

          Hi Jacob,

           

          It is possible to assign the same complete handler for N loader instances but I'm not sure if this is the correct way to do it. If I'd have to store text and multiple images that relate to that text then I'd assign an ID to the images that match the ID of the text and link them that way OR I'd zip the hole piece and store it as an archive. When something is needed, I'd read the content of the archive assuring this way that everything that is found inside the archive has something to do with the text.

           

          With best regards,

          Barna Biro

          • 2. Re: multiple loaders, one complete event
            Starlover_jacob Level 1

            well.. i am using amfphp to store the data into a mysql database.

            this way i can easily search and show data from the server inside my air app.

             

            Your way to zip it all and send it to the server is not a bad idea..

            I only have to figure out if amfphp can handle that..

            and ofcorse unpack that zip.. store the files in the correct folder.

            Read a text file and store all that data into the mysql database..

             

            pff.. difficult stuff for someone who is building his first air app..

             

            Greets Jacob.

            • 3. Re: multiple loaders, one complete event
              Barna Biro Level 3

              Hehe, yeah, the first few steps are always the hardest. I'm not saying that my method would be the best out there, maybe someone else will come around and provide a much simpler and cleaner way to do this.

               

              Wishing you luck,

              Barna Biro

              • 4. Re: multiple loaders, one complete event
                Michael Borbor Level 4

                Just an idea you could send it using byte array. If you're using 

                RemoteObject then your data is already compressed so I don't think you 

                need to zip it.

                 

                Forgive me if I misunderstood something but Adobe Forums doesn't make 

                your life easy when you use a cell phone.

                 

                Sincerely,

                 

                Michael

                 

                El 11/05/2009, a las 7:42, Starlover_jacob <forums@adobe.com> escribió:

                 

                >

                well.. i am using amfphp to store the data into a mysql database.

                this way i can easily search and show data from the server inside my 

                air app.

                >

                Your way to zip it all and send it to the server is not a bad idea..

                I only have to figure out if amfphp can handle that..

                and ofcorse unpack that zip.. store the files in the correct folder.

                Read a text file and store all that data into the mysql database..

                >

                pff.. difficult stuff for someone who is building his first air app..

                >

                Greets Jacob.

                >

                • 5. Re: multiple loaders, one complete event
                  Starlover_jacob Level 1

                  hi,

                  yess, I was thinking about something like a bytearray, but then I would need a loader and on the load complete, I'll convert it to a bytearray right?

                   

                  The thing is I have 2 images. so I have some difficulty understanding how I can load both images.. convert them to 2 bytearrays. and then send it along with all the textual data to the server all at once.

                   

                  Zipping it was more a way to hold all things together. (textual data, and 2 images)

                   

                  How to save the bytearray into files again on the server side is another problem witch I'll research later.

                   

                  and what did you mean by;

                  Forgive me if I misunderstood something but Adobe Forums doesn't make

                  your life easy when you use a cell phone.

                   

                  I am building an air app just for on a windows or mac.

                  I am not building something for a cell phone. If that's what you meant by it

                  • 6. Re: multiple loaders, one complete event
                    Michael Borbor Level 4

                    By that I meant that I'm using my cell phone to read post and publish 

                    post to Adobe Forums so I could've probably misunderstood something.

                     

                    You could create a VO and wrap the two images inside the object and 

                    then send the VO to your server, I dunno if this works but I'd give it 

                    a shot.

                    Sincerely,

                     

                    Michael

                     

                    El 11/05/2009, a las 8:41, Starlover_jacob <forums@adobe.com> escribió:

                     

                    >

                    hi,

                    yess, I was thinking about something like a bytearray, but then I 

                    would need a loader and on the load complete, I'll convert it to a 

                    bytearray right?

                    >

                    The thing is I have 2 images. so I have some difficulty 

                    understanding how I can load both images.. convert them to 2 

                    bytearrays. and then send it along with all the textual data to the 

                    server all at once.

                    >

                    Zipping it was more a way to hold all things together. (textual 

                    data, and 2 images)

                    >

                    How to save the bytearray into files again on the server side is 

                    another problem witch I'll research later.

                    >

                    and what did you mean by;

                    Forgive me if I misunderstood something but Adobe Forums doesn't make

                    your life easy when you use a cell phone.

                    >

                    I am building an air app just for on a windows or mac.

                    I am not building something for a cell phone. If that's what you 

                    meant by it

                    >

                    • 7. Re: multiple loaders, one complete event
                      Muzak Level 3
                      The thing is I have 2 images. so I have some difficulty understanding how I can load both images

                       

                      Those images are where ?

                       

                      If I'm not mistaken you said you have url's to images in an SQLite db. If so, you can load those into your app using File (if they're local files).

                      Once they're loaded you can do whatever with them, including sending them to a server using remoting.

                       

                      There's no need for zips or whatever.

                      Transform the image bitmapdata to bytearray (check PNGEncoder and JPEGEncoder in the docs, or google).

                       

                      You can send/load multiple data using remoting, you're not limited to sending 1 image at a time or one piece of text at a time (if that's what you were thinking).

                      You can send/load arrays, objects, etc..

                       

                      If you're looking for some OOP info, google ValueObject (VO) and/or Data Transfer Object (DTO).

                      • 8. Re: multiple loaders, one complete event
                        Starlover_jacob Level 1

                        Hi,

                         

                        You are totally right.. the images are locally.. the path to that is saved into my sqlite db.

                         

                        The problem is the point where i load my images.

                        I need to know when both of my images are done loading, so i can convert it to bytearray and send it to the server.


                        As far as i know, i need a loader to load an image.. and when the complete event goes off i can do something with that image.

                        (make it a bytearray and stuff like that)

                        That works fine for one image... but 2 images?

                         

                        Greets, Jacob

                        • 9. Re: multiple loaders, one complete event
                          Muzak Level 3

                          The problem is the point where i load my images.

                          I need to know when both of my images are done loading, so i can convert it to bytearray and send it to the server

                           

                          Ah, I see. Well, you got 2 options:

                          - user 2 loaders and keep track of when both are loaded

                          - use 1 loader and load 1 after the other.

                           

                          A 3rd option would be to create a custom loader class tailored to your needs. The custom loader class would do the above (load both images) and when done displatch 1 complete Event.

                          You would then do something along these lines:

                           

                          var imgLoader:ImageLoader = new ImageLoader();
                          imgLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
                          imgLoader.load(thumbURL, fullURL);
                          


                          Make sense?

                          • 10. Re: multiple loaders, one complete event
                            Starlover_jacob Level 1

                            Yes i understand,and it sounds perfect for me.. a custom loader class.

                            but i never did that kind of stuff.. creating a custom loader class.

                             

                            How would that look like?

                             

                            i presume that in my customloader class i have a function called load..

                            what would that function do? call a new loader with my first image?(myloader.load(new URLRequest(encodeURI(imageurl)));)

                            In my opinion i then again need 2 loaders..

                            How can i keep track of the loaders and know if there both loaded?

                             

                            Greets, Jacob

                            • 11. Re: multiple loaders, one complete event
                              Starlover_jacob Level 1

                              Well guys, i found my answer.

                               

                              all credits go to this page:

                              http://blog.molanphydesign.com/2008/08/17/loading-multiple-images-in-as3/

                               

                              The code for my solution:

                              var imagePath:Array =  [“image1.jpg”, “image2.jpg”, “image3.jpg”];
                              var imageArr:Array = new Array();
                              
                              loadImage(imagePath[0]);
                              
                              private function loadImage(img:String):void 
                              {
                                   var imageLoader:Loader = new Loader();
                                   imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
                                   imageLoader.load(new URLRequest(img));
                              }
                              
                              private function loadComplete(e:Event):void 
                              {
                                   imageArr.push(e.target.content);
                                   imagePath.shift(); //remove the first item in the array.
                                   if (imagePath.length > 0) //if there is an image left in the array we load it
                                   {
                                        loadImage(imagePath[0]);
                                   }
                                   else // no images left in the array so we process the images.
                                   {
                                        trace("all images are loaded");
                                   }
                              }
                              

                               

                              This did the trick.. quite simple actually

                               

                              Greets, Jacob