Posted By

martijnbleyen on 06/30/10


Tagged

class image component


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

debacelar


ImageComponent


 / Published in: ActionScript 3
 

  1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.Loader;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.IOErrorEvent;
  8. import flash.events.ProgressEvent;
  9. import flash.net.URLRequest;
  10.  
  11. public class ImageComponent extends Sprite
  12. {
  13. ////////////////////////
  14. // HOW TO USE
  15. ////////////////////////
  16. // var imgComp:ImageComponent = new ImageComponent()
  17. // addChild(imgComp);
  18. // imgComp.image = "http://url.com/image.jpg";
  19. // OR a bitmap that is already loaded in
  20. // ////////////////////
  21. //HOW TO SET MaxHeight / MaxWidth
  22. //////////////////////
  23. // imgComp.maxHeight = 200;
  24. // the width will scale in function of the maxheight set.
  25. ///////////////////////
  26. // imgComp.maxWidth = 200;
  27. // the height will scale in function of the maxwidth set.
  28. //////////////////////
  29.  
  30. private var imageLoader:Loader;
  31. private var _image:Object;
  32. private var _img:Bitmap;
  33. private var _maxWidth:uint;
  34. private var _maxHeight:uint;
  35. public function ImageComponent()
  36. {
  37.  
  38.  
  39. }
  40.  
  41. public function get image():Object
  42. {
  43. return _image;
  44. }
  45.  
  46. public function set image(value:Object):void
  47. {
  48. _image = value;
  49. if(_image is Bitmap)
  50. {
  51. // If it's a bitmap that is already loaded, add it to displaylist
  52. _img = image as Bitmap;
  53. _img.smoothing = true;
  54. addChild(_img);
  55. }else if(_image is String)
  56. {
  57. //If it's a string, check the file extension and then load it.
  58.  
  59. //make a string from the last 4 characters in the filename string.
  60. var imageExtension:String = new String(image.substring(image.length-4,image.length));
  61.  
  62. if(imageExtension == ".jpg" || ".png" || ".gif" || ".bmp"){
  63. //if the string is one of these four file extensions then Load in the image.
  64. if(_img != null)
  65. {
  66. removeChild(_img);
  67. }
  68. imageLoader = new Loader();
  69. imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  70. imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  71. imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, IOerrorHandler);
  72. imageLoader.load(new URLRequest(image as String));
  73.  
  74. }else{
  75. // If it's not one of the four file extensions, trace an error
  76. trace("IMG ERROR : "+image+" is not an image!");
  77.  
  78. }
  79. }else
  80. {
  81. trace("This is not an image!");
  82. }
  83.  
  84.  
  85. }
  86.  
  87. private function completeHandler(e:Event):void
  88. {
  89. //Image is loaded, now add to displayList
  90. _img = e.target.loader.content as Bitmap;
  91.  
  92. addChild(_img);
  93. _img.smoothing = true;
  94.  
  95.  
  96. }
  97.  
  98. private function progressHandler(e:ProgressEvent):void
  99. {
  100. trace("Bytes loaded :: "+e.bytesLoaded);
  101. }
  102.  
  103. private function IOerrorHandler(e:IOErrorEvent):void
  104. {
  105. trace("can't find file");
  106. }
  107.  
  108.  
  109. // GETTER SETTERS
  110. public function get maxHeight():uint
  111. {
  112. return _maxHeight;
  113. }
  114.  
  115. public function set maxHeight(value:uint):void
  116. {
  117. _maxHeight = value;
  118. _img.height = _maxHeight;
  119. _img.scaleX = _img.scaleY;
  120. }
  121.  
  122. public function get maxWidth():uint
  123. {
  124. return _maxWidth;
  125. }
  126.  
  127. public function set maxWidth(value:uint):void
  128. {
  129. _maxWidth = value;
  130. _img.width = _maxWidth;
  131. _img.scaleY = _img.scaleX;
  132. }
  133.  
  134.  
  135.  
  136. }
  137. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Winkyboy on May 18, 2011

Regarding Flex, using this code: http://stackoverflow.com/questions/261386/how-to-put-an-image-say-png-on-a-graphics-in-flex-3

Posted By: Winkyboy on May 19, 2011

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);

Posted By: Winkyboy on May 19, 2011

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);

You need to login to post a comment.