Can't load image in swf - actionscript-3

I get error with load image into swf file. Here is my code:
private var loader:Loader = new Loader( );
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
addChild(loader);
}
e very thing is OK, logo of Google appeared. But I need to manipulate pixel the image have loaded. I add a image Bitmap :
private var loader:Loader = new Loader( );
private var image:Bitmap;
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
this.addChild(image);
}
There is nothing appear. I can't find any solution on internet. I try and detect :
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
//after above line, rest of function never run
addChild(image); // <-- no run ##
trace("this no run"); // <-- no run ##
}
I use Flash Builder 4 and have no error or any warning.
Can somebody show me a solution, please?
Thank for reading.

You can't access pixel data of an image if image is in different domain than your application (and there is no crossdomain policy file)

1 You need crossdomain.xml on Google site :)
2 You need use checkpolicy:
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"), context);
but it works only if Google add crossdomain.
3 You can use little serverside proxy on your domain. Flash call proxy to get file with your url, proxy download it and return to flash...

Related

Load external SWF loader content not accessible

Actually i have a SWF that is loaded by another SWF.
This SWF that is loaded, itself load another SWF.
This may be weird, but it's in a context of an online game that let you develop SWF as plugins.
So, we have SWFA(the video game) ---> SWFB ----> SWFC
When i try to load the external SWFC, the COMPLETE event fire, but i am only able to add it to the stage by adding directly the Loader, because the loader doesn't have any content property.
public function load(){
loader = new Loader();
loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
applicationContext = new LoaderContext(true, ApplicationDomain.currentDomain);
loader.load(getRequest(),applicationContext);
}
private function getRequest():URLRequest {
Security.allowDomain("https://mock.com");
Security.loadPolicyFile("https://mock.com/crossdomain.xml");
var req:URLRequest = new URLRequest("https://mock.com/mock.swf");
req.method = URLRequestMethod.POST;
req.data = new URLVariables("name=kk");
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("user:pass");
var credsHeader:URLRequestHeader = new
URLRequestHeader("Authorization", "Basic " + encoder.toString());
req.requestHeaders.push(credsHeader);
return req;
}
Here is the onComplete function
public function onComplete (e:Event):void{;
this.addChild(e.target.content); // this is null
this.addChild(e.currentTarget.content); // null too
}
So, when i run my SWF from my local computer, everything goes well, e.target.content contain my external SWF.
But when i run the same within the SWF Loader inside the online game, i can't find any content property. Plus, childAllowParent property of LoaderInfo is always false.
Does someone have an idea of what's going on?
Thanks

Flash-Animated-GIF-Library

I'm trying to use the Flash-Animated-GIF-Library. It serves to load an animated gif. It does it with the fileReference class, where you have to browse your folders, choose an animated gif and then it'll show it on the stage. I need the animated gif to show without that browsing part. Is it in anyway possible to use that class to load animated gifs directly, the same way you'd load and show an image with the Loader class? If so - how?
Yes, you have 2 options.
Use the Loader or URLLoader class. Example1, Example 2 (get bytearray)
Embed the image and get ByteArrayAsset. Example
The minimal code for option1 (Loader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"));
}
private function loaderComplete(event:Event):void
{
    var loaderInfo:LoaderInfo = LoaderInfo(event.target);
    var byteArray:ByteArray = loaderInfo.bytes;     
player.loadBytes(byteArray);
}
The minimal code for option1 (URLLoader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"))
}
private function loaderComplete(event:Event):void
{
player.loadBytes(event.target.data);
}
And for option2:
[Embed(source="yourgif.gif",mimeType="application/octet-stream")]
public var YourGif:Class;
protected function handleCreationComplete(event:FlexEvent):void
{
var byteArrayAsset:ByteArrayAsset = new YourGif();
player.loadBytes(byteArrayAsset);
// should work, too
//player.loadBytes(new YourGif() as ByteArray);
}

AS3 image loading content is always null

I have a problem with my assets loader I've created for my flash app. In a function I try to load an external image and return it from this function. My problem is that I always get loader.content set to null. This how my code looks like:
public static function loadImage(path:String):BitmapData
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, check);
if (path == "")
loader.load(new URLRequest("images/default.png"));
else
loader.load(new URLRequest("images/" + path));
var image:Bitmap = new Bitmap((loader.content as BitmapData));
return image.bitmapData;
}
public static function check(e:Event):void
{
trace("Loading completed");
e.target.removeEventListener(Event.COMPLETE, check);
}
Any ideas???? I've also included my event listener which does launch.
I'm pretty sure it's because the loader works asynchronously, but I have no idea how to modify it to be able to return the image from the function.
Thanks in advance!
I'm pretty sure it's because the loader works asynchronously
Correct
but I have no idea how to modify it to be able to return the image from the function
There is no way to do that.
Why?
Because it is asynchronous.
You can't return a result of a request straight after the request has been made.
The execution of the code does not pause and wait for the response to come back, so your function will hit the return statement before you get the data, and that is why it is returning null.
The only way to handle this is to listen for the response (Event.COMPLETE) and run a callback function after the response from the server has been received.
So following your code example, you could do:
private static var _callback :Function;
public static function loadImage(path:String, callback:Function):void
{
_callback = callback;
// ....
and then:
public static function check(e:Event):void
{
// ...
_callback( whateverYouWantToReturnGoesHere );
This should work assuming that you only have one image loaded at a time (as your methods are static), otherwise you will need to implement some sort of request queue handling.
I hope this helps.
Your getting a 'null' because you're referring to loader.content too soon. Here's a simplified version that works, just fine, in a timeline:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, check);
loader.load(new URLRequest("images/default.png"));
function check(e:Event):void
{
var bmd:BitmapData = new BitmapData(195,130,false,0xFFFFFFFF)
var image:Bitmap = new Bitmap(bmd);
bmd.draw(loader.content);
trace(loader.content);
trace("Loading completed");
addChild(image);
e.target.removeEventListener(Event.COMPLETE, check);
}
You can put it back into a form suitable for your classes.
You should say:
var bitmapData:BitmapData;
and
var image:Bitmap = new Bitmap(event.target.content.bitmapData)
instead of
var image:Bitmap = new Bitmap((loader.content as BitmapData));

AS3 URLRequest not working in browser

My URL request is working perfectly in Flash Pro, but when I test it in the Browser, the image doesn't work at all. It doesn't load. Is there a Publish Setting I need to be using or something?
Please help, it's pretty frustrating. Here's what it looks like when it's in the browser.
Relevant code:
public function startSlidingPuzzle() {
// blank spot is the bottom right
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
// load the bitmap
loadBitmap("http://fc07.deviantart.net/fs71/f/2014/030/d/7/slidingimage_by_nooodisaster-d74et6t.jpg");
}
// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}
// bitmap done loading, cut into pieces
public function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
trace(numPiecesHoriz)
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
You have a security error:
SecurityError: Error #2122: Security sandbox violation: Loader.content: http://fc03.deviantart.net/fs71/f/2014/030/e/e/beyonce_slidingpuzzle___flash_diary_day_10_by_nooodisaster-d74er8h.swf cannot access http://i.stack.imgur.com/ls9QI.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at SlidingPuzzle/loadingDone()
Try adding a security context:
public function loadBitmap(bitmapFile:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
context.checkPolicyFile = true;
var request:URLRequest = new URLRequest( bitmapFile );
loader.load( request, context );
}
If that doesn't fix it you'll need to add a crossdomain.xml to the server hosting the images you are requesting. Why not save the images locally and deploy them in the same scope as your build?
[1]:

How to upload a background image using actionscript 3.0 code?

Need your help. I am trying it first time.
I have used following code.
But i get this in my console:
started loading file
SecurityError: Error #2000: No active security context.
and my image url is in same folder as my script file.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.load(new URLRequest("C:\Documents and Settings\Owner\Desktop\23Aug\demo1\mic.jpg"), context);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
trace("started loading file");
addChild(loader);
function fileLoaded(event:Event):void
{
trace("file loaded");
}
Reasons to throw securityError exception.
Invalid path,
Trying to access a URL, that not permitted by the security sandbox,
Trying a socket connection, that exceeding the port limit, and
Trying to access a device, that has been denied by the user(Ex., camera, microphone.)
try this
private var _loader:Loader = new Loader();
private var _context:LoaderContext = new LoaderContext();
private var _url:URLRequest = new URLRequest("demo1/mic.jpg");
_context.checkPolicyFile = false;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageloaded);
//_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_loader.load(_url, _context);
private function onImageloaded(e:Event):void{
addChild(e.target.content);
}
Use slashes instead of back slashes :
loader.load(new URLRequest("C:/Documents and Settings/Owner/Desktop/23Aug/demo1\mic.jpg"), context);
But the best way is to use relative path like "./mic.jpg" and do not use absolute path.