Shared Object Save BitmapData
Source code:
http://www.kirupa.com/forum/showthread.php?306972-Saving-bitmaps-to-a-shared-object
/**
* Copyright freshMan ( http://wonderfl.net/user/freshMan )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/rgRc
*/
package {
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.SharedObject;
import flash.net.URLRequest;
import flash.utils.ByteArray;
/**
* Save bitmap image to local shared object
*/
public class SOTest extends Sprite {
private var log:Log;
private var _so:SharedObject;
private var _image:Loader;
private var _saveBytes:Boolean = true;
public function SOTest():void {
log = new Log(0xFF0000);
addChild(log);
log.text = "Text";
log.y = stage.stageHeight - log.height;
_image = new Loader();
_image.contentLoaderInfo.addEventListener(Event.COMPLETE, displayImage);
//http://board.flashkit.com/board/showthread.php?808707-A-policy-file-is-required-but-the-checkPolicyFile-flag-was-not-set-when-this-media-w&p=4229955#post4229955
Security.loadPolicyFile('http://api.facebook.com/crossdomain.xml');
Security.allowDomain('http://http://wonderfl.net/');
Security.allowInsecureDomain('http://http://wonderfl.net/');
_so = SharedObject.getLocal("bmpholder");
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.applicationDomain = ApplicationDomain.currentDomain;
if (_so.data.img == null) {
log.text = ("loaded from jpg file");
//
_image.load(new URLRequest("http://www.gstatic.com/webp/gallery/1.jpg"),context);
} else {
log.text = ("loaded from byte array saved to SharedObject");
_image.loadBytes(_so.data.img, context);
_saveBytes = false;
}
}
private function displayImage(event:Event):void {
addChild(_image);
if (_saveBytes) saveImage();
}
private function saveImage():void {
var data:BitmapData = new BitmapData(_image.width, _image.height);
data.draw(_image);
var encoder:JPGEncoder = new JPGEncoder();
var bytes:ByteArray = encoder.encode(data);
_so.data.img = bytes;
_so.flush();
}
}
}
import flash.text.TextField;
import flash.text.TextFormat;
class Log extends TextField
{
public function Log(color:uint)
{
var f:TextFormat = new TextFormat();
f.font = "_sans";
f.size = 12;
f.color = color;
f.align = "left";
setTextFormat(f);
defaultTextFormat = f;
autoSize = "left";
wordWrap = false;
multiline = false;
selectable = false;
}
}