Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Saving loaded PNG files to a File, and loading saved PNG files from a File

Community Beginner ,
Aug 25, 2017 Aug 25, 2017

I'm developing an iPad app that tries to load live images from a url. If it succeeds, it also caches those images in the ApplicationStorageDirectory. If it can't find the images at the URL (presumably because the user is offline), it tries to load any previously cached images from the ApplicationStorageDirectory. If the images haven't been previously cached, if finally looks in the ApplicationDirectory for images I'm including inside the .ipa as included files.

It successfully loads the images when online. It seems to successfully load fallback images from the ApplicationDirectory. But it does not seem to load cached images from the ApplicationStorageDirectory. I've been spinning my wheels, and this discussion seems to have the answers, but I must be doing something wrong. Any help would be greatly appreciated. Thanks!

public var loader:ProLoader;

private var imageID:String;

private var appStorage:File;

private var appDir:File;

private var loadErrorRepeat:int;

private var bytes:ByteArray;

public function Modal() {

     // ...

     loader = new ProLoader();

     loader.x = 575;

     loader.y = 143;

     addChild(loader);

     loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaderCompleteHandler);

     loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageLoaderErrorHandler);

     appStorage = File.applicationStorageDirectory.resolvePath("images/floorplans/");

     appDir = File.applicationDirectory.resolvePath("images/floorplans/");

     bytes = new ByteArray();

     // ...

}

private function imageLoaderCompleteHandler( e:Event ):void

{

     trace("imageLoaderCompleteHandler()");

     //This is the image file. We'll be saving it on png format

     var imgFile:File = appStorage.resolvePath(imageID);

     //trace(imgFile.nativePath +" already exists? "+imgFile.exists);

     //trace(imgFile.url +" already exists? "+imgFile.exists);

     //This is the FileStream object you'll be using to write the image data to the folder

     var fileStream:FileStream = new FileStream();

     //var byteArray:ByteArray = PNGEncoder.encode( Bitmap(ProLoaderInfo( e.currentTarget ).content).bitmapData )

     var byteArray:ByteArray = ProLoaderInfo( e.currentTarget ).bytes;

     fileStream.open(imgFile, FileMode.WRITE);

     fileStream.writeBytes(byteArray);

     fileStream.close();

     trace("cached image as "+imgFile.url);

}

private function imageLoaderErrorHandler( e:IOErrorEvent ):void

{

     trace("dataLoaderErrorHandler()");

     if(++loadErrorRepeat > 1) // make sure I don't get caught in an infinite error loop

     {

          loadErrorRepeat = 0;

          return;

     }

     if(appStorage.resolvePath(imageID).exists) // if I've already cached the online image

     {

          loader.load( new URLRequest(appStorage.resolvePath(imageID).url));

          trace("retrieved image from "+appStorage.resolvePath(imageID).url);

          /*var stream:FileStream = new FileStream( );

          stream.addEventListener(Event.COMPLETE, streamCompleteHandler);

          stream.open(appStorage.resolvePath(imageID), FileMode.READ);

          stream.readBytes(bytes);*/

     }

     else // if I haven't cached the online image, use the one bundled with the app

     {

          loader.load(new URLRequest("images/floorplans/" + imageID));

          trace("retrieved image from images/floorplans/" + imageID);

          //loader.load(new URLRequest(appDir.resolvePath(imageID).url));

          //trace("retrieved image from "+appDir.resolvePath(imageID).url);

     }

}

/*private function streamCompleteHandler( e:Event ):void

{

loader.loadBytes(bytes);

trace("retrieved image from "+appStorage.resolvePath(imageID).url);

(e.currentTarget as FileStream).close( );

}*/

private function loadImage( param:String ):void

{

     trace("loadImage( " + param + " )");

     loader.unload();

     imageID = param.substr( param.lastIndexOf("/") + 1, param.length);

     loader.load( new URLRequest(param));

     trace("imageID: "+imageID);

     trace(appStorage.resolvePath(imageID).url + " already exists? " +  appStorage.resolvePath(imageID).exists);

     trace(appDir.resolvePath(imageID).url + " already exists? " +  appDir.resolvePath(imageID).exists);

}

TOPICS
ActionScript
677
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 25, 2017 Aug 25, 2017
LATEST

I assume that the problem involves either incorrectly encoding or decoding the PNG files. But that's just an assumption.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines