<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlexOut &#187; caching</title>
	<atom:link href="http://flex.exposedout.net/tag/caching/feed/" rel="self" type="application/rss+xml" />
	<link>http://flex.exposedout.net</link>
	<description>i am an itemrendrer in the making!</description>
	<lastBuildDate>Fri, 13 May 2011 10:05:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Caching images in Flex</title>
		<link>http://flex.exposedout.net/2009/05/25/caching-images-in-flex/</link>
		<comments>http://flex.exposedout.net/2009/05/25/caching-images-in-flex/#comments</comments>
		<pubDate>Mon, 25 May 2009 09:27:54 +0000</pubDate>
		<dc:creator>Vikram singh</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[bitmapData]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[hashmap]]></category>
		<category><![CDATA[image]]></category>

		<guid isPermaLink="false">http://guavus.wordpress.com/?p=226</guid>
		<description><![CDATA[Caching of  image data  in your Flex application is one of the best ways to avoid loading those large images again and again, and this  can  improve performance and reduce overhead of loading external resources. We are not discussing about using the cacheAsBitmap property to improve rendering performance or the cachePolicy property to speed up [...]


Related posts:<ol><li><a href='http://flex.exposedout.net/2011/05/13/export-to-jpeg-in-flex-using-screen-capture/' rel='bookmark' title='Permanent Link: Export to Jpeg in flex.'>Export to Jpeg in flex.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Caching of  image data  in your Flex application is one of the best ways to avoid loading those large images again and again, and this  can  improve performance and reduce overhead of loading external resources.</p>
<p>We are not discussing about using the <span style="color:#008000;">cacheAsBitmap </span>property to improve rendering performance or the <span style="color:#008000;">cachePolicy </span>property to speed up animations. Its the caching of actual bitmap data of an image. As the 2 are different in one case we just cache the image data of an image that has to be animated and component doesnot reload any image. in the other case we  reload the data on demand  for example user profile with display picture. In this case the picture mostly does not change (well it does but  we can handle it differently) so we dont need to reload the image at each time the user profile data of the same person is called.. so we can impliment a cache here.</p>
<p>You’ll need a hash map to store the image data. A Dictionary or an associative array will also work just fine. Loading an image for the first time is the same as usual. You create a new Image object and add a listener for the COMPLETE event or it could be any of your custom event for the sake of example we will follow :</p>
<blockquote><p>var image : Image = new Image ();<br />
image.addEventListener (Event.COMPLETE, onImageLoad);</p></blockquote>
<p>Once the image has finished loading, you add a copy of the bitmap data to the hash map using the image URL as the hash key as the URL for any image will always be distinct:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> imageCache : hashMap = <span style="color: #000000; font-weight: bold;">new</span> hashMap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #808080; font-style: italic;">// create hashmap only once</span><br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onImageLoad <span style="color: #66cc66;">&#40;</span>event : Event<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">var</span> image : Image = event.<span style="color: #0066CC;">target</span> as Image;<br />
<span style="color: #000000; font-weight: bold;">var</span> imageURL : <span style="color: #0066CC;">String</span>  = image.<span style="color: #006600;">source</span>;<br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> imageCache.<span style="color: #006600;">containsKey</span> <span style="color: #66cc66;">&#40;</span>imageURL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">var</span> bitmapData : BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<br />
<span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">content</span>.<span style="color: #0066CC;">width</span>, image.<span style="color: #006600;">content</span>.<span style="color: #0066CC;">height</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
bitmapData.<span style="color: #006600;">draw</span> <span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">content</span><span style="color: #66cc66;">&#41;</span>;<br />
imageCache.<span style="color: #006600;">put</span> <span style="color: #66cc66;">&#40;</span>imageURL, bitmapData<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The above method is called and the image is cached in the hash map. Now you can use the image as many times as we want without ever having to load it again. A good exapmle of this could be  loading user profile whenever you reload the profile data one need not fetch the  user profile picture. the picture can directly be accessed from the hashmap.</p>
<p>How do we use the image we stored, Its simple you need to check the hash map each time you call for the loading the image just check if you’ve already cached it:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// check the hashmap when ever next you want to load the image</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>imageCache.<span style="color: #006600;">containsKey</span> <span style="color: #66cc66;">&#40;</span>imageURL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
image.<span style="color: #006600;">source</span> = <span style="color: #000000; font-weight: bold;">new</span> Bitmap <span style="color: #66cc66;">&#40;</span>imageCache.<span style="color: #006600;">getValue</span> <span style="color: #66cc66;">&#40;</span>imageURL<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>thats it now you can use the cache whenever required.</p>


<p>Related posts:<ol><li><a href='http://flex.exposedout.net/2011/05/13/export-to-jpeg-in-flex-using-screen-capture/' rel='bookmark' title='Permanent Link: Export to Jpeg in flex.'>Export to Jpeg in flex.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://flex.exposedout.net/2009/05/25/caching-images-in-flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

