Flex:如何调整图像大小?

时间:2014-04-18 14:33:17

标签: flash flex flash-builder

我尝试了多种方法,但它没有用。我的应用程序允许用户上传多个图像,然后用户可以通过单击一个按钮调整所选图像的大小 的代码:

public function resizeImage():void
{ 
  var tempArray:ArrayCollection = new ArrayCollection(listOfSelectedImages.source);
                var originalImage:Image = tempArray.getItemAt(0) as Image;
                var resizedImage:Image = new Image();
                var resizeObj:Resize = new Resize();

                resizedImage = originalImage;

                /* for each(var originalImage:Object in tempArray)
                { */

                //}
                resizedImage.maxWidth = 470
                resizedImage.maxHeight = 394;
                resizedImage.smooth = true;
                saveFiles(resizedImage);
            }

            private function saveFiles(image:Image):void
            {
                    //var image:Image = resizedArray.getItemAt(0) as Image;
                    var byteArray:ByteArray = convertImageToByteArray(image.source);
                    fileReference.save(byteArray, image.name.split(".")[0]+".jpeg");
            }

我尝试使用调整大小效果组件更改调整大小逻辑:

var resizeObj:Resize = new Resize();

                resizedImage = originalImage;
                resizeObj.widthTo=470;
                resizeObj.heightTo = 394;
                resizeObj.widthFrom = originalImage.width;
                resizeObj.heightFrom = originalImage.height;
                resizeObj.target = resizedImage;

                resizeObj.end();
                resizeObj.play();
                /* for each(var originalImage:Object in tempArray)
                { */

                //}

                resizedImage.smooth = true;

但对我来说没什么用。

  
    

这里我试图调整一个图像的大小以进行测试,但最后我将调整多个图像的大小。

  

1 个答案:

答案 0 :(得分:0)

使用answer中的Resize images in Flex using as3

public function scaleBitmap(src: BitmapData, ratio: Number): BitmapData
{
    var bmd: BitmapData = new BitmapData(src.width * ratio, src.height * ratio);
    var m: Matrix = new Matrix();   
    m.scale(ratio, ratio);
    bmd.draw(src, m);
    return bmd;
}

然后你的循环看起来像:

var scaleWanted:Number = 0.5; //get the real ratio desired from user input
var originalImages:ArrayCollection; //set this from selected images
var resizedImages:ArrayCollection = new ArrayCollection();
for each (var img:Image in originalImages) {
    var resizedData:BitmapData = scaleBitmap(img.bitmapData), scaleWanted);
    var alteredImage:Image = new Image();
    alteredImage.source = resizedData;
    resizedImages.push(alteredImage);
}
//next up, save the resized images to disk.