闪存上的外部图像未加载到位图中

时间:2009-02-19 15:29:09

标签: flash

之前我有一个答案,它非常有帮助,因此我修改了我的代码以使其工作,但是,我仍然在square-1

//load libs
import flash.net.*;
import flash.geom.Matrix;
import flash.display.*;
import flash.events.*;
import com.adobe.images.JPGEncoder;

function myLoader() {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch);

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg");
    loader.load(request);
}

//action for mouse 
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);
Mouse.hide();

function moveCursor(event:MouseEvent):void{
    pencil.x = event.stageX;
    pencil.y = event.stageY;
}


var canvas_mc;
    canvas_mc = new MovieClip()

addChildAt(canvas_mc,0);
canvas_mc.swapDepths

//draw area to sketch
function sketch(e:Event):void{
    //load bitmap and draw it to memory
    var myBitmapData;
        myBitmapData = new BitmapData(500, 500);
        myBitmapData.draw(e);

    //define matrix
    var matrix;
        matrix = new flash.geom.Matrix();

    //start canvas
    canvas_mc.graphics.beginBitmapFill(myBitmapData,matrix, true, true);
    canvas_mc.graphics.drawRect(0, 0, 500, 500);
    canvas_mc.graphics.endFill();

    //listening  events
    canvas_mc.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
    canvas_mc.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
    canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine);    
}       

//mouse draws on press
function startDrawing(event:MouseEvent):void{   
    canvas_mc.graphics.lineStyle(1, 0, 1);
    canvas_mc.graphics.moveTo(mouseX, mouseY);
    canvas_mc.addEventListener(MouseEvent.MOUSE_MOVE, makeLine);
}

//mouse stops drawing on realese
function stopDrawing(event:MouseEvent):void{
    canvas_mc.removeEventListener(MouseEvent.MOUSE_MOVE, makeLine);
}

//creates lines
function makeLine(event:MouseEvent):void{
    canvas_mc.graphics.lineTo(mouseX, mouseY);  
}

//call function
myLoader()

//start to save onto server
var serverPath:String = "";
function createJPG(m:MovieClip, q:Number, fileName:String){
    var jpgSource:BitmapData = new BitmapData (m.width, m.height);
    jpgSource.draw(m);
    var jpgEncoder:JPGEncoder = new JPGEncoder(q);
    var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
    var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
    var jpgURLRequest:URLRequest = new URLRequest ( serverPath+"jpg_encoder_download.php?name=" + fileName + ".jpg");       
    jpgURLRequest.requestHeaders.push(header);              
    jpgURLRequest.method = URLRequestMethod.POST;               
    jpgURLRequest.data = jpgStream;

    var jpgURLLoader:URLLoader = new URLLoader();   
    //jpgURLLoader.load(jpgURLRequest);     
    navigateToURL(jpgURLRequest, "_blank");
}

save_btn.addEventListener(MouseEvent.CLICK, saveBtnPress);
save_btn.addEventListener(MouseEvent.ROLL_OVER, saveBtnOver);
save_btn.addEventListener(MouseEvent.ROLL_OUT, saveBtnOut);

function saveBtnPress(e:Event):void{    
createJPG(canvas_mc, 90, "sketch");
}

function saveBtnOver(e:Event):void{ 
    Mouse.show();
    pencil.visible = false;
}

function saveBtnOut(e:Event):void{  
    Mouse.hide();
    pencil.visible = true;
}

2 个答案:

答案 0 :(得分:1)

我不确定问题是什么,但我假设您正在尝试绘制myLoader()函数加载的图像,并且您没有看到图像。在这种情况下,问题是您没有将Loader实例作为子项添加到主精灵中。这是修复:

function myLoader() {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sketch);

    var request:URLRequest = new URLRequest("http://www.sergiorodriguez.org/images/2008_5_FXD_VividBlack.jpg");
    loader.load(request);
    addChild(loader);
}

说明:加载器最终将加载已加载的图像,但是如果要显示它,则需要显示加载器本身。

答案 1 :(得分:0)

您无法将事件绘制到位图。您需要绘制加载的图像

改变你的行:

myBitmapData.draw(e);

myBitmapData.draw(loader);

我怀疑你的代码有很多其他问题,我无法真正看到你在任何地方将BitmapData添加到任何Bitmap,所以你永远不会看到画出的内容。

此外,尝试在您的问题中更具体一点,如果我们不必阅读整个代码,那么回复会更容易。