How to convert loaded SWF to bitmapdata? - actionscript-3

I use the loader class to load some png files to the stage. The function that is called after the file is loaded is something similar to this:
private function IconLoaded(e:Event):void
{
percentLoaded_txt.visible = false;
iconLoader = Loader(e.target.loader);
icone = Bitmap(iconLoader.content);
icone.smoothing = true;
var _icon: imgBox;
_icon = new imgBox(_MAX_WIDTH, _MAX_HEIGHT, icone,0);// new imgHelper(_bitmap);
_icon.x = _main.cena.width/2;
_icon.y = _main.cena.height/2;
addChild(_icon);
}
imgBox is a class that auto resize the bitmap and allow also to add border for example...
My code works fine, to load PNG files. But I want also to be able to load swf files.
How can I do this?
Thanks.

You will need to make a copy of the loaded SWF into a Bitmap.
function copyIt(obj:DisplayObject):Bitmap {
var bd:BitmapData = new BitmapData( obj.width, obj.height );
bd.draw(obj);
return new Bitmap(bd);
}
var bmpCopy:Bitmap = copyIt(mySWF);
addChild(bmpCopy);

You can't load a swf as a bitmap data because they are totally distinct types. What you can do is load a swf and add it as a child of a movie clip to show its content.
This link shows how to load a swf. After loading it, you just need to add this as a child of a MovieClip and add this movieclip to the stage to show its content.

Related

Add bitmap to stage from CameraRoll loaded image

I need to use the code below to load an image from the CameraRoll with Adobe Air on iOS 8. (it will also be used to read the EXIF data from the loaded image)
I would like to add the bitmap via addChild() to the stage as soon as the onMediaLoadedCameraRoll function gets triggered. How to do that?
var loaderCameraRoll:Loader
var deviceCameraRoll:CameraRoll
var dataSourceCameraRoll:IDataInput;
var mediaPromiseCameraRoll:MediaPromise;
function loadImageFromCameraRoll(e:Event=null):void {
deviceCameraRoll = new CameraRoll();
deviceCameraRoll.addEventListener(MediaEvent.SELECT, onSelectCameraRoll);
deviceCameraRoll.browseForImage();
}
function onSelectCameraRoll(event:MediaEvent):void {
mediaPromiseCameraRoll = event.data;
dataSourceCameraRoll = mediaPromiseCameraRoll.open();
var eventSource:IEventDispatcher = dataSourceCameraRoll as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onMediaLoadedCameraRoll );
}
function onMediaLoadedCameraRoll(event:Event):void {
// display loaded image
}
The documentation says this about the matter:
The data property is a MediaPromise object, which you can load using the loadFilePromise() method of the Loader class.
This is followed by an example that does exactly that:
var imagePromise:MediaPromise = event.data;
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
imageLoader.loadFilePromise( imagePromise );
As seen in the example code, you should always add the listeners for a Loader to its contentLoaderInfo property.

Loading google maps static images into flash

I'm trying to load images from google maps static images api into flash. When I attempt to load them flash complain about sandbox issues. Even after I try and load the policy file from google.
Security.loadPolicyFile("http://maps.googleapis.com/crossdomain.xml");
Is there any way around this? or is it simply impossible to load images in this fashion?
var loader1:Loader = new Loader();
var lctx1:LoaderContext = new LoaderContext(true);
loader1.contentLoaderInfo.addEventListener(Event.INIT, doImgInit);
loader1.load(new URLRequest("http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=600x400&maptype=satellite&sensor=false&scale=4"), lctx1);
private function doImgInit(evt:Event):void
{
trace("DO IT")
// get a reference to the LoaderInfo object in which the image is loaded
var lInfo:LoaderInfo = evt.target as LoaderInfo;
// this variable is used as reference to the image in the end.
var dO:DisplayObject;
// try to access the "content" property of the loader, if it works, there is a crossdomain.xml file.
try{
dO = lInfo.loader.content;
}
// if there wasn't one, we need to put the loader inside another object in order to manipulate it
catch(err:SecurityError)
{
// create a new Sprite to contain the loaded image
var sprt:Sprite = new Sprite();
// add the loader to the sprite
sprt.addChild(lInfo.loader);
// get a reference to this sprite in the dO variable
var dO:DisplayObject = sprt as DisplayObject;
}
// from here on you can do anything to the dO variable, rotate it, draw it unto a bitmapData, move it around..
// but first don't forget to add to to some container that is on the stage so you can see it!
}

AS3 externally loaded swf to control stage and MovieClips and their children?

I have 2 swf files. One is loaded and loading an externally loaded swf into it.
Here is the code in the first loaded swf: #1
var logo:Loader = new Loader();
logo.load(new URLRequest("images/Logo.png"));
logo.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadLogo);
function LoadLogo(e:Event):void
{
addChild(logo);
}
/// The SWF I AM LOADING
var Beau:Loader = new Loader();
Beau.load(new URLRequest("Beau.swf"));
Beau.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadBeau);
function LoadBeau(e:Event)
{
addChild(Beau);
}
NOW HERE IS THE CODE FOR THE BEAU SWF LOADED: #2
import flash.display.MovieClip;
bird.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
//// I WANT TO BE ABLE TO ADD CODE TO CONTROL MOVIECLIPS
//// AND CHILDREN form this externally loaded swf. How do
//// I do that? Below code does not work.
var logo:MovieClip;
var ControlLogo:MovieClip = MovieClip(logo.content);
ControlLogo.alpha = .3;
}
EDIT
ok your code and tutorials are great. I am having major SANDBOX issues now when I make the clip load from an external URL. I am also coding for an ANDROID using FLASH CS5.5. Its not allowing me to use Security.allowDomain("*");
BOTTOME LINE IS THE NEWLY LOADED SWF can not access the PARENT WITHOUT PERMISSION BUT I DON'T KNOW HOW TO GIVE IT PERMISSION.... using the AIR/ANDROID PLAYER.
use property parent
parent gives loader
parent.parent gives swf#1
So you can write function as given below
function fl_MouseClickHandler(event:MouseEvent):void
{
var swf1:Object = parent.parent;
var logo:Loader = swf1.logo;
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
ControlLogo.alpha = .3;
}
make sure that logo is not a local variable, but global and public in swf#1
public var logo:Loader = new Loader();
EDIT:
logo:Loader gets added to SWF#1. Loads image, so content is Bitmap.
beau:Loader gets added to stage. Loads swf (SWF#2), so content is MovieClip.
so now
root is SWF#1
parent of beau is SWF#1
parent of SWF#2 is beau and parent of beau is SWF#1
so for SWF#2, SWF#1 is parent of parent so parent.parent
If you are not creating public variables, you can manage this by using property name.
var logo:Loader = new Loader();
logo.name = "logo";
...
...
var beau:Loader = new Loader();
beau.name = "beau";
...
...
Then anywhere in swf#2
var swf1:Object = parent.parent;
var logo:Loader = Loader(swf1.getChildByName("logo"));
....
....
For accessing the content it is recommended to use type casting as I have shown
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
so that you can check as shown below and avoid runtime errors
var ControlLogo:Bitmap = Bitmap(logo.content);
if(ControlLogo){
}
If you want to so something irrespective of content do as shown below
var ControlLogo:Object = logo.content;
if(ControlLogo && ControlLogo.hasOwnProperty("alpha")){
ControlLogo.alpha = 0.4;
}

AS3 loading image from Array into empty movieclip

I have tried this to load image path from database via php and load the image in flash. Now how can i add child to every single image to a every single empty movie clip for the farther use. the code is
var ldr:URLLoader = new URLLoader(new URLRequest("m_shirts.php"));
ldr.addEventListener(Event.COMPLETE, _done);
function _done(e:Event):void {
ldr.removeEventListener(Event.COMPLETE, _done);
var ar:Array = String(e.target.data).split("#");
for (var x:int=0; x<ar.length; x++){
/*var img:Loader = new Loader();
img.load(new URLRequest(ar[x]));
shirts.addChild(img);*/
var myLoader:Loader = new Loader();
var my_mc:MovieClip = new MovieClip();
myLoader.load(new URLRequest(ar[x]));
my_mc.addChild(myLoader);
addChild(my_mc);
my_mc.x= my_mc.width + 50;
}
}
Ok, I will try to answer this from what I understand:
You simply cannot convert Loader instances to a MovieClip. A Loader is a Loader and a MovieClip is a MovieClip, much like an Array is an Array or a Function is a Function. Their common base class is DisplayObjectContainer. It has most of the functionality MovieClips have except for child manipulation (although the methods are there) and drawing API as per the graphics property.
You can access the size of the loaded image by reading the Loader's width and height.
Your problem seems to be, that you want to determine the image's size before it is loaded. You cannot do that. You have to wait for it to load first. The contentLoaderInfo will dispatch an Event.COMPLETE when the image is available.

How do I prevent a Loader from changing my apps position and size?

I load an SWF using a simple loader but it changes the main applications x/y position and size.
Which loader do I use to load an swf that will maintain the position size of my app?
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, function handleInit(event:Event):void{
var UIDesigner:UIComponent = new UIComponent();
UIDesigner.addChild(event.target.loader.content);
UIDesigner.id = "id_sample";
ApplicationObject.Instance.addElementAt(UIDesigner,0)
event.target.content.addEventListener(FlexEvent.APPLICATION_COMPLETE, function applicationCompleteHandler(event:Event):void{
var app:SystemManager = event.target as SystemManager;
var Designer:SkinnableContainer = app.application as SkinnableContainer;
Designer.id = "id_test";
ApplicationObject.Instance.removeElementAt(0);
pTabCon.addChild(Designer as SkinnableContainer);
pTabCon.horizontalScrollPolicy = "off";
pTabCon.verticalScrollPolicy = "off";
Parent.id_ComponentsTab.addChild(pTabCon);
Parent.id_ComponentsTab.selectedChild = pTabCon;
trace("swf loaded successfully");
ApplicationObject.Instance.m_ApplicationList.addItem(Designer);
});
});
The problem is not the Loader but how you use it and specifically, how you add the content to the stage.
A DisplayObjectContainer will change size according to its content. For instance , if you load a MovieClip with a width of 400 in another MovieClip with a 200 width, the containing MovieClip width will change to 400. This is an expected behavior.
It's possible to modify this behavior by using masking for instance or modifying the loaded content properties before adding content to the stage.
As for the x & y properties, it would have to do with your code. Loading content shouldn't change the x & y properties of the containing Object.
If you edit your question and add some code example we may be able to give you more specific help.
does the swf that you load call root or maybe stage? i've had such issue with an swf animated background for my app.
and how do load it?
upd: you can always use NO_SCALE
loadSWFFile("resources/awords/myswf.swf");
private function loadSWFFile(fileURL:String):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleSWFLoadComplete0);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler0);
loader.load( new URLRequest(fileURL));
}
private function handleSWFLoadComplete0( evt:Event ):void
{
loaderWidth = evt.currentTarget.loader.width;
loaderHeight = evt.currentTarget.loader.height;
// Now set the width and height of target component same as loaderWidth and loaderHeight respectively.
}
private function ioErrorHandler0(e:IOErrorEvent):void
{
mx.controls.Alert.show("Unable to load", "IOError", 0);
}