当我尝试保存已缩放的裁剪图像时,我的保存功能停止工作,但在图像未缩放时工作完全正常。有什么想法吗?
这是我的代码:
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;
}
答案 0 :(得分:0)
在裁剪方法中,您是否尝试过绘制它而不传递原始DisplayObject的变换矩阵?在我看来它不应该传递变换矩阵,特别是因为你想按原样复制DisplayObject的缩放版本。
另外,在你的裁剪方法中,在绘制你要复制的显示对象之前创建位图似乎有些倒退(我不认为这是弄乱它的但是它可能值得一看...... )
我会这样做:
var bmd:BitmapData = new BitmapData (_width, _height, true, 0x00000000);
bmd.draw (displayObject, null, null, null, rect, true);
return new Bitmap (bmd, PixelSnapping.ALWAYS, true);