/ Published in: ActionScript 3
Expand |
Embed | Plain Text
package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLRequest; public class ImageComponent extends Sprite { //////////////////////// // HOW TO USE //////////////////////// // var imgComp:ImageComponent = new ImageComponent() // addChild(imgComp); // imgComp.image = "http://url.com/image.jpg"; // OR a bitmap that is already loaded in // //////////////////// //HOW TO SET MaxHeight / MaxWidth ////////////////////// // imgComp.maxHeight = 200; // the width will scale in function of the maxheight set. /////////////////////// // imgComp.maxWidth = 200; // the height will scale in function of the maxwidth set. ////////////////////// private var imageLoader:Loader; private var _image:Object; private var _img:Bitmap; private var _maxWidth:uint; private var _maxHeight:uint; public function ImageComponent() { } public function get image():Object { return _image; } public function set image(value:Object):void { _image = value; if(_image is Bitmap) { // If it's a bitmap that is already loaded, add it to displaylist _img = image as Bitmap; _img.smoothing = true; addChild(_img); }else if(_image is String) { //If it's a string, check the file extension and then load it. //make a string from the last 4 characters in the filename string. var imageExtension:String = new String(image.substring(image.length-4,image.length)); if(imageExtension == ".jpg" ||Â ".png" ||Â ".gif" || ".bmp"){ //if the string is one of these four file extensions then Load in the image. if(_img != null) { removeChild(_img); } imageLoader = new Loader(); imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler); imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOerrorHandler); imageLoader.load(new URLRequest(image as String)); }else{ // If it's not one of the four file extensions, trace an error trace("IMG ERROR : "+image+" is not an image!"); } }else { trace("This is not an image!"); } } private function completeHandler(e:Event):void { //Image is loaded, now add to displayList _img = e.target.loader.content as Bitmap; addChild(_img); _img.smoothing = true; } private function progressHandler(e:ProgressEvent):void { trace("Bytes loaded :: "+e.bytesLoaded); } private function IOerrorHandler(e:IOErrorEvent):void { trace("can't find file"); } // GETTER SETTERS public function get maxHeight():uint { return _maxHeight; } public function set maxHeight(value:uint):void { _maxHeight = value; _img.height = _maxHeight; _img.scaleX = _img.scaleY; } public function get maxWidth():uint { return _maxWidth; } public function set maxWidth(value:uint):void { _maxWidth = value; _img.width = _maxWidth; _img.scaleY = _img.scaleX; } } }
Comments
Subscribe to comments
You need to login to post a comment.

Regarding Flex, using this code: http://stackoverflow.com/questions/261386/how-to-put-an-image-say-png-on-a-graphics-in-flex-3
Here we go. Better, more direct and appropriate use of imageComponent in Flex:
var imgComp:ImageComponent = new ImageComponent(); imgComp.image = "http://www.largeimage.com/users/guest/Hidden%20Hummingbird.thb.jpg"; var uic:UIComponent = new UIComponent(); uic.addChild(imgComp); YOUR-UI-COMPONENT-GOES-HERE.addChild(uic);
SIGH. sorry about the lack of linebreaks... hopefully better here:
var imgComp:ImageComponent = new ImageComponent(); imgComp.image = "http://www.largeimage.com/users/guest/Hidden%20Hummingbird.thb.jpg"; var uic:UIComponent = new UIComponent(); uic.addChild(imgComp); hbxDetailImageContainer.addChild(uic);