(ActionScript 3) Generate low res thumbnails
I am trying to create an image gallery similar to Androids "Gallery" where it is a horizontal scrolling list of images. I am doing this with an HBox and Bitmap objects wrapped in a UIComponent. The problem is I am trying to do this for mobile and having many full size images open in memory causes problems. I have been trying to have the images load right as they are scrolling into view and unload once they are out of view, but the loading/unloading while scrolling requires too much cpu and causes a very choppy and poor UI.
What I am looking to do is be able to load several low quality versions of the images in memory so I dont have to do as much loading/unloading. I have tried decreasing the height/width but it dosnt seem to have an effect on memory usage. Does anyone know an AS library or class that can help me?
Any help would be greatly appreciat开发者_如何学JAVAed. Thank you
You can create new bitmapdatas of the size you desire, and use the bitmapdata.draw() function to make smaller thumbnails. The trick is to use a transform matrix when you call draw to scale the image size down.
Here is teh link to the Matrix class: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html
and here is teh link to bitmapData.draw() http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#draw()
If this is not 100% intuitive to you, I recommend making a simple swf that just takes an image and then uses BitmapData.draw(myImage, myMatrix); to create a scaled down copy. Once you ahve it worked out with the right amount of scaling, you can copy it into you mobile app.
I don't know about what Griffin said is true or not but what I would do is take the bitmapdata and draw it into another bitmapdata object with the final height and width to what you want then use matrix to scale in the original image while you draw to the new bitmap.
Once you have the new bitmap made you can then delete and remove all references to the original bitmap which should allow it to be GCed.
If you go here you can read up on how to scale a matrix.
Changing the width and height doesn't mean much memory wise, since the same image file is being loaded.
Actionscript can't mainpulate the image files themselves, only the instance of the data in the flash app. (i.e. you can resample a bitmap data object all you like, but you still have the original file in memory).
You need some server side language to resize the images for you. Like PHP with the GD library.
Thank you for all the responses. I have been playing with this and there doesn't seem to be a difference is memory being used by AIR. I am looking for a way to keep a small, low res version of the image in memory, since having a few full res images in memory on a mobile device uses up all the resources.
精彩评论