Why do Loader objects kill bitmapdata draw();?

北战南征 提交于 2019-12-06 15:24:55

问题


The code below is a simple add and save program. You press the addButton to add content to the canvas and than press the exportButton to save an image of the canvas in its current state to your desktop. This code works fine when I add circle objects to the canvas that are drawn to the canvas. The problem lies when I try to load a .png into the canvas. Once the loaded content is added to the canvas the exportButton stops working all together and no longer responds. What is causing the loaded content to break the code?

**This POST HASS BEEN EDITED AS OF MARCH 30, 2012 LISTED BELOW IS THE WORKING CODE*

public class Application extends MovieClip
{
    public var canvas:MovieClip = new MovieClip();
    public var addBtn:MovieClip = new MovieClip();
    public var exportBtn:MovieClip = new MovieClip();
    public var _content:BitmapData;

    public function Application()
    {
        canvas.graphics.beginFill(0x333333);
        canvas.graphics.drawRect(0,0,450,450);
        canvas.graphics.endFill();
        addChild(canvas);

        addBtn.graphics.beginFill(0x999999);
        addBtn.graphics.drawRect(0,0,50,50);
        addBtn.graphics.endFill();

        exportBtn.graphics.beginFill(0x999999);
        exportBtn.graphics.drawRect(0,0,50,50);
        exportBtn.graphics.endFill();

        canvas.x = stage.stageWidth / 2 - canvas.width;
        addBtn.x = exportBtn.x = canvas.x +(canvas.width +25);
        exportBtn.y = addBtn.height + 25;

        addBtn.addEventListener(MouseEvent.CLICK, addClick);
        exportBtn.addEventListener(MouseEvent.CLICK, exportClick);

        addChild(canvas);
        addChild(addBtn);
        addChild(exportBtn);
    }
    private function addClick(event:MouseEvent):void
    {
        var loader:Loader = new Loader();
        loader.load(new URLRequest("alphabet/a.png"));
        loader.x = Math.random() * canvas.width;
        loader.y = Math.random() * canvas.height;
        canvas.addChild(loader);
    }
    private function exportClick(event:MouseEvent):void
    {
        _content = new BitmapData(canvas.width,canvas.height);
        _content.draw(canvas);
        saveBitmap();
    }
    private function saveBitmap():void
    {
        var encodedContent:ByteArray = PNGEncoder.encode(_content);
        var fileWindow:FileReference = new FileReference();
        fileWindow.save(encodedContent, "Image_randomly.png");
    }
}

}


回答1:


The problem may be crossdomain security issues. If you are loading from a site other than your own, check to make sure it has a crossdomain policy at domain.com/crossdomain.xml. Also set the LoaderContext (the second argument of load). Something like this: loader.load("http://www.example.com/myimage.jpg", new LoaderContext(true)); Otherwise, you can load images from other sites but not access the actual bitmapData, which a draw() requires.

Also, you should install a debug player on at least one browser and run it there and see if there are any errors.



来源:https://stackoverflow.com/questions/9883850/why-do-loader-objects-kill-bitmapdata-draw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!