从精灵内部的掩码下面保存图像

时间:2011-05-31 15:39:30

标签: flash actionscript-3

我有一个sprite对象,它会在其中加载一个自定义形状,用作蒙版,以便从使用加载器对象加载的图像中剪切出自定义形状 - 加载器(loada)和形状(线)对象都是精灵对象的子对象 - 我添加了代码来保存掩码下面的图像但是当调用保存函数时,没有任何东西被保存掉,并且不会抛出任何错误。

function save(evt:MouseEvent):void{
    //Create random number from current date/time to name image created
    var imgID:Number= new Date().getTime()
    //Matrix to holder the area to be cropped
    var maskRect = loada.mask.getBounds(lines);
    //Matrix of image to be cropped
    var imgMatrix= loada.mask.transform.matrix;
    //Cropped image
    var myCroppedImage:Bitmap = cropImage(maskRect, imgMatrix, loada.mask.width, loada.mask.height, loada.mask );
    //Get new matrix of cropped image
    var m:Matrix = myCroppedImage.transform.matrix;
    var urlLoader:URLLoader = new URLLoader();
    //Set jpg quality of the image to be export 1-100
    var myEncoder:JPGEncoder = new JPGEncoder(80);
    //Create jpg to be exported
    var jpgSource:BitmapData = new BitmapData (loada.mask.width, loada.mask.height);
    jpgSource.draw(myCroppedImage, m);
    //Create byte array to hold jpg data
    var byteArray:ByteArray = myEncoder.encode(jpgSource);
    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    trace(imgID);
    //Send image to the server to be saved
    var saveJPG:URLRequest = new URLRequest("save_image.php?r="+imgID);
    saveJPG.requestHeaders.push(header);
    saveJPG.method = URLRequestMethod.POST;
    saveJPG.data = byteArray; 
    trace(saveJPG);
}

//Crop image
function cropImage( rect, matrix, _width:Number, _height:Number, displayObject:DisplayObject):Bitmap {
    //Create cropped image
    var croppedBitmap:Bitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.ALWAYS, true );
    croppedBitmap.bitmapData.draw(displayObject, matrix , null, null, rect, true );
    return croppedBitmap;
}

1 个答案:

答案 0 :(得分:0)

根据您在此处发布的内容,您似乎没有调用“加载”功能:

urlLoader.load(saveJPG);