loading saved bitmap - actionscript-3

i had saved a bitmap and i want to load it in runtime.
here is my codes:
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1;
textClass = getDefinitionByName(textName) as
Class;
var tx:BitmapData = new textClass(); txtP[1] = new
Bitmap(tx);
saveDataTxt.data.txtArray[1] = txtP[1];
addChild(saveDataTxt.data.txtArray[n]);
but it gives me an error :
**TypeError: Error #1034: Type Coercion failed: cannot convert Object#384c2b1 to flash.display.DisplayObject.**
whats the solution?

To store a bitmap in a shared object, you would need to serialize it to a byte array first (see Is it possible to store images in the SharedObject of Flash?)
What you can do, is just store your custom BitmapData subclass in the shared object (if you don't want to bother with byte arrays)
//you need to register every class/subclass in your shared object
registerClassAlias("flash.display.BitmapData", BitmapData);
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1; textClass = getDefinitionByName(textName) as Class;
registerClassAlias(textName,textClass); //need to register the custom class
var tx:BitmapData = new textClass(); txtP[1] = new Bitmap(tx);
saveDataTxt.data.txtArray[1] = tx; //just store the bitmap data
addChild(new Bitmap(saveDataTxt.data.txtArray[n] as BitmapData)); //you have to cast the object as bitmap data

Related

How to take a snapshot of a chart and save into Byte Array in flex?

I'm trying to capture snapshot of a chart and save that image as a Byte Array in flex. currently i'm using this method to save a snapshot but now i want to save image in a Byte array. Is this possible ?
this is my current method
private function takeSnapshot():void{
var image:ImageSnapshot = ImageSnapshot.captureImage(chart);
var file:FileReference = new FileReference();
var fileName:String = "chart.png";
file.save(image.data,fileName);
This is how I would do in AS3:
// displayObject being the object to render
var data:BitmapData = new BitmapData(displayObject.width, displayObject.height, true, 0);
data.draw(displayObject);
// the you can have either a raw bytearray or a png:
var bitmapBa:ByteArray = data.getPixels( new Rectangle(0, 0, displayObject.width, displayObject.height);
// or if you're saving to PNG:
var pngBA:ByteArray = PNGEncoder.encode( data );

Flex Mobile AS3, PersistenceManager storing/receiving BitmapData

I'm using the PersistenceManager to store data from my App. If I store BitmapData, it will stored correctly, but after a restart the BitmapData is now an Object. If I cast the Object to BitmapData it doesn't work.
pm.setProperty("sign", signArea.getBitmapData());
And this is the way I try to load it.
pm.getProperty("sign") as BitmapData;
If I doesn't stop the app, it would loaded correctly, but after a restart the "sign" is not BitmapData anymore. It's now an Object.
I don't think you can safely store an instance of BitmapData in a shared object (internally used in the PersistanceManager). It is not explicitly mentioned in the docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
You can however save the data of the BitmapData as a ByteArray and convert is back when retrieving.
// write
var byteArray:ByteArray = bitmap.bitmapData.getPixels(bitmap.bitmapData.rect);
so.data.byteArray = byteArray;
so.data.width = bitmap.bitmapData.rect.width;
so.data.height = bitmap.bitmapData.rect.height;
so.flush();
// read
var byteArray:ByteArray = so.data.byteArray;
var bitmapData:BitmapData = new BitmapData(so.data.width, so.data.height);
bitmapData.setPixels(bitmapData.rect, byteArray);
Notice that you'll also need to store the width and the height of the image. You could wrap this in an object so that you have 1 entry in the persistance manager instead of 3. This might be more convenient if you want to store multiple bitmaps.
// write
var bd:BitmapData = signArea.getBitmapData();
var r:Rectangle = bd.rect;
pm.setProperty("sign", {bytes:bd.getPixels(r), width:r.width, height:r.height});
// read
var signData:Object = pm.getProperty("sign");
var bitmapData:BitmapData = new BitmapData(signData.width, signData.height);
bitmapData.setPixels(bitmapData.rect, signData.bytes);

URLLoader requests data format to be Text but it gets Binary issue?

I have a URLLoader which normally requesting the data to be in Text format, anyway on a specific conditions it might gets a Binary as result, which is actually just 1 integer number in binary format. Anyway the conversation :
var ba : ByteArray = ( e.currentTarget as URLLoader ).data; // failing on this row.
var r : int = ba.readInt();
doesnt passing anymore since the result data is in text forma due the request header...
How to convert the result data into integer ?
if You are sure that You recive int there , type :
var value:int= int(( e.currentTarget as URLLoader ).data);
Same thing You can do if You get string or XML .
But if You like to recive data as ByteArray , You should type before load on URLLoader line :
import flash.net.URLLoaderDataFormat;
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
than Your data will be bytearray.
Simply, you need to change
urlLoader.dataFormat=URLLoaderDataFormat.BINARY
But before you send a request. After, there's no point doing that.
EDIT
To read text into a binary (ByteArray)
var ba:ByteArray=new ByteArray();
ba.writeUTFBytes(( e.currentTarget as URLLoader ).data);
ba.position=0;
var myInteger:int=ba.readInt();
function test(){
var bytes:ByteArray = new ByteArray();
bytes.writeInt(0x00DDAA99); //create my byte array with int 14527129
bytes.position = 0; //move the postion to the start
var newInt:int = bytes.readInt(); //read the bytes from starting position
trace("new num: "+newInt); //print out the number
}
Basic reading and writing an integer to byte array

How do you combine two bytearrays in AS3?

I'm trying to combine two ByteArrays to send it out as post data using URLRequest.. Whenever I try to simply add them up, the request becomes GET instead of POST and the data for some reason doesn't get included.
create a total ByteArray by adding other ByteArray objects to it via the writeBytes() public method of the ByteArray class.
more info here: Reading and writing a ByteArray
Combining / Concatination Two Byte Arrays
Var Data:ByteArray = new ByteArray();
Var Salt:ByteArray = new ByteArray();
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);//Defines the **length of Resultant Array**
//Array Copy Method(VB)/ Concate the ByteArray(ActionScript) one After another
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
I will Show here Conversion of String into Byte Array and Merging Them (concate /combining) them into single byte array
// In Detail
var HashOut:ByteArray = new ByteArray();
var byterrData:ByteArray = new ByteArray();
var byterrSalt:ByteArray = new ByteArray();
//conversion of string Data and Salt in respective (UTF-8 and Default) Byte Array
var Data:String = "password";
var Salt:String ="‰ô©³¶í"; //Using Special Characters in a String variable
byterrData.writeMultiByte(Data, "iso-8859-1");
byterrSalt.writeMultiByte(Salt,Salt);
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);
// Concate the ByteArray
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
//Now You can Trace It by using
trace(DataAndSalt[0]);
trace(DataAndSalt[index Number]);
Not sure what your code looks like... the GET/POST issue is very weird.
However, use the below instead of trying to "add them up" (whatever that means).
array3 = array1.concat(array2);

Dynamically Instancing Objects ActionScript 3.0

I have a variable named "type". And I want to instance an object with the name of the value of type. Here is an example:
var myObjectName = "ball";
var object = new ball(); //Except I want to use the value of myObjectName.
I believe this used to be easy with AS2 when using _global, but I'm not sure how to do it in AS3?
Any help?
First get the class object with flash.utils.getDefinitionByName(), then instantiate that object:
var myClass:Class = getDefinitionByName(myObjectName) as Class;
var object:Object = new myClass();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getDefinitionByName()