Search code examples
actionscript-3shared-objects

Save image loaded to my UILoader in AS3 in order to load it without internet connection


I've got some UIloader in my AS3 code that displays images via web link.

uiloader1.source = "http://www.mywebsite/image1.jpg";
uiloader2.source = "http://www.mywebsite/image2.jpg";

Once the user have open the app with an internet connection, I'd like him to be able to open it again without any internet connection but still have the possibility to display the images.

I thought about something like :

var savedImage:SharedObject;
savedImage= SharedObject.getLocal("ImagesFolder");

if (monitor.available || savedImage.data.images == "null") {

    uiloader1.source = "http://www.mywebsite/image1.jpg";
    uiloader2.source = "http://www.mywebsite/image2.jpg";

    savedImage.data.loader1 = uiloader1.source;
    savedImage.data.loader2 = uiloader2.source;

}

else {

    uiloader1.source = savedImage.data.loader1;
    uiloader2.source = savedImage.data.loader2;

}

But it doesn't work as it saves the link of the image and not the actual image.

Any idea how can I save the images into a file and told uiloader1.source and uiloader2.source to load them ?

EDIT

So I've tried that :

uiloader10.source = "http://www.myWebsite/image1.jpg";
uiloader10.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void {

    var bitmapData:BitmapData = new BitmapData(uiloader10.width,uiloader10.height);
    bitmapData.draw(uiloader10);
    var jpgencoder:JPGEncoder = new JPGEncoder(100);
    var mybyte:ByteArray = jpgencoder.encode(bitmapData);
    var myfile:FileReference = new FileReference();

    myfile.save(mybyte,"test.jpg");

}

It seems to work (test it on desktop, not on my phone device yet).

1/But you think I shouldn't do that for an AIR app ? Could you show me the code I should change in order to make it more "app compatible" please ?

2/ How do I do to load the file in a Uiloader (how can retrieve automatically the path ?" Example : I've tried uiloader11.source = "test.jpg"; but error : Unhandled ioError:. text=Error #2035: URL Not Found. URL: app:/test.jpg


Solution

  • Of course it stores the URL as you instructed, why should it do otherwise?

    You can store a limited list of data types in SharedObject: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#data

    So, after loading your actual images from the web you are to store their content as a ByteArray which could be found in Loader.loaderInfo.bytes: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#loaderInfo http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/LoaderInfo.html#bytes

    If that does not work you have plan B to load image source with URLLoader by setting URLLoader.dataFormat to URLLoaderDataFormat.BINARY. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html#dataFormat

    Then on the second start you can restore these images from SharedObject with Loader.loadBytes() method: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#loadBytes()

    P.S. If your app runs under AIR environment you actually can read/write files to local storage rather than spam SharedObject with data that are not supposed to go there. Use File.applicationStorageDirectory to locate the app storage folder and FileStream,open(), FileStream.readBytes(), FileStream.writeBytes() to open, read, write files respectively.

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html#applicationStorageDirectory http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html