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

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.

This entry was posted in ActionScript, Flex. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

10 Comments

  1. Posted November 14, 2007 at 12:13 am | Permalink

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

  2. bhu
    Posted May 6, 2008 at 10:27 pm | Permalink

    Very nice post, thank you

  3. Posted September 3, 2008 at 9:50 am | Permalink

    Thanks! This is just what I was looking for.

  4. KT
    Posted October 28, 2008 at 8:21 am | Permalink

    That is very helpful! Thanks!

  5. evanflash
    Posted November 20, 2008 at 9:31 am | Permalink

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

    thanks for the post.

  6. Alice
    Posted March 4, 2009 at 6:11 am | Permalink

    Hi,

    This is a great example, but I was wondering if it is possible that I can resize the image as I drag it and release the mouse, instead of pre-defining the constants of how much the image is to be resized.
    Does anyone have any suggestions on this?

  7. Posted August 27, 2009 at 1:16 pm | Permalink

    So how do you get the dimensions of the loaded image?

  8. Posted August 27, 2009 at 2:45 pm | Permalink

    This is definitely a great solution. If you want to take care of the vertical spacing and still use a VBox, it’s as simple as nesting the Image inside a Box element and setting a padding left.

  9. Posted August 27, 2009 at 2:49 pm | Permalink

    To simplify my previous post, it looks like horizontalAlign for a VBox with an image does work correctly now. They must have read your entry!

  10. Nuwan
    Posted January 29, 2010 at 4:33 am | Permalink

    Thank you ,
    this was really helpful!!!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word