Vixiom Axioms

November 2, 2007

Flex Image Component: Loading, Resizing, Aligning, and Smoothing

Filed under: ActionScript, Flex Alastair @ 11:04 am

Here’s the brief load an external image into Flex, resize it to fit in a container (but don’t make it bigger than the original), center it in the container, and smooth the image to get rid of any aliasing. Sounds like a simple job, slap down a VBox, set vertical and horizontal alignment, add an Image component and set it’s width and height to 100%, grab some handy image smoothing code, you’re done no? No. There’s a issue with every one of those steps.

First up the VBox. If a VBox contains a dynamic image (who’s size is undermined) the VBox will no pick up the Image component’s new size once the image has loaded so it can’t adjust the image to be centered properly.

Next is the Image component, setting the Image Component’s width and height to 100% seems like a good idea until you get an image with smaller dimensions than it’s container. The image will stretch to fit and become blurry. Setting the scaling to false will then not allow images that are larger than the container to scale down. We’ll have to handle image resizing manually.

Finally smoothing, dynamically loaded images do not smooth when you change their size. The solution is to use Ben Longoria’s image smoothing component but it if you don’t set your security model property your app will still crash and burn as you can’t get an image’s properties if the Flash player barfs a security sand box violation (no smoothing and no resizing as you can’t even get contentWidth or contentHeight).

Anyways long story short here is my solution to the problem:

  1. Don’t set any image component properties before hand, once it’s loaded use it’s ‘complete’ event to resize it.

  2. Use a Canvas instead of a VBox and handle positioning by hand
  3. Set a loaderContext for the image, and make sure your cross domain policy is set up correctly

Here’s an example. The top image isn’t centered correctly and it’s aliased because it’s a regular Image component (you can see jaggies on the curves and angles). Right click to view source.

Digg! submit Flex Image Component: Loading, Resizing, Aligning, and Smoothing to stumbleupon.com submit Flex Image Component: Loading, Resizing, Aligning, and Smoothing to del.icio.us submit Flex Image Component: Loading, Resizing, Aligning, and Smoothing to reddit.com Like this post? subscribe to the feed.

5 Comments »

  1. Wow, that is funny you just wrote this blog entry because this exactly solves a current issue of mine. Thanks a lot!

    Comment by Ryan Campbell — November 14, 2007 @ 12:13 am

  2. Very nice post, thank you

    Comment by bhu — May 6, 2008 @ 10:27 pm

  3. Thanks! This is just what I was looking for.

    Comment by Allen Manning — September 3, 2008 @ 9:50 am

  4. That is very helpful! Thanks!

    Comment by KT — October 28, 2008 @ 8:21 am

  5. “There’s a issue with every one of those steps.”
    That line made me lol — typical flex :-)

    thanks for the post.

    Comment by evanflash — November 20, 2008 @ 9:31 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress