简体   繁体   English

裁剪 actionscript-3 时缩放图像不保存

[英]Scaled image not saving when cropped actionscript-3

When i try to save a cropped image that i have scaled, my save function ceases to work, but works perfectly fine when the image is not scaled.当我尝试保存已缩放的裁剪图像时,我的保存 function 停止工作,但在图像未缩放时工作得很好。 Any ideas?有任何想法吗?

Here's my code:这是我的代码:

var loada = new Loader();
loada.load(new URLRequest("img_one.jpg"));
loada.x = 205;
loada.y = 110;
loada.cacheAsBitmap = true;
loada.scaleX = 1.5;


function toCrop():void 
{
    //Matrix to holder the area to be cropped
    var maskRect = loada.mask.getBounds(loada);
    //Matrix of image to be cropped
    var imgMatrix= loada.mask.transform.matrix;
    //Cropped image
    var myCroppedImage:Bitmap = crop(maskRect, imgMatrix, loada.mask.width, loada.mask.height, loada );

    var m:Matrix = myCroppedImage.transform.matrix;

    var urlLoader:URLLoader = new URLLoader();
    //Set jpg quality of the image to be export 1-100
    var myEncoder:PNGEncoder = new PNGEncoder();
    //Create jpg to be exported
    var pngSource:BitmapData = new BitmapData (loada.mask.width, loada.mask.height, true, 0x00000000);
    pngSource.draw(myCroppedImage, m);
    //Create byte array to hold jpg data
    var byteArray:ByteArray = PNGEncoder.encode(pngSource);
    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    //Send image to the server to be saved
    var savePNG:URLRequest = new URLRequest(glo.bal.base_url+"save_png.php?r="+glo.bal.fb_id+glo.bal.image_extension);
    savePNG.requestHeaders.push(header);
    savePNG.method = URLRequestMethod.POST;
    savePNG.data = byteArray;
    urlLoader.addEventListener(Event.COMPLETE, doNothing1);
    urlLoader.load(savePNG);
}

function doNothing1(e:Event):void
{
    trace ( e );
}

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

In your crop method, have you tried drawing it without passing the original DisplayObject's transform matrix?在您的裁剪方法中,您是否尝试过在不传递原始 DisplayObject 变换矩阵的情况下绘制它? Seems to me it shouldn't need the transform matrix passed, especially since you want to copy the scaled version of the DisplayObject as-is.在我看来,它不应该需要传递变换矩阵,特别是因为您想按原样复制 DisplayObject 的缩放版本。

Also, in your crop method, it seems a little backwards to create the bitmap before drawing the displayobject you want to copy ( I don't think this is what is messing it up but it might be worth a look...)此外,在您的裁剪方法中,在绘制要复制的显示对象之前创建 bitmap 似乎有点倒退(我认为这不是搞砸的原因,但它可能值得一看......)

I'd do:我会做:

var bmd:BitmapData  = new BitmapData (_width, _height, true, 0x00000000);
bmd.draw (displayObject, null, null, null, rect, true);
return new Bitmap (bmd, PixelSnapping.ALWAYS, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM