<?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>Hive05 software products &#187; crop</title>
	<atom:link href="http://www.hive05.com/tag/crop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hive05.com</link>
	<description>...hive05 is alive...</description>
	<lastBuildDate>Fri, 05 Feb 2010 00:18:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
    <title>Hive05 software products</title>
    <url>http://www.hive05.com/feed-logo.png</url>
    <link>http://www.hive05.com</link>
    <width>88</width>
    <height>31</height>
    <description>Hive05 software products - http://www.hive05.com</description>
    </image>		<item>
		<title>Code Sample: Crop an image using the iPhone SDK</title>
		<link>http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/</link>
		<comments>http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 07:10:20 +0000</pubDate>
		<dc:creator>Josh M</dc:creator>
				<category><![CDATA[Game Trivia Catechism]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[core-image]]></category>
		<category><![CDATA[crop]]></category>

		<guid isPermaLink="false">http://www.hive05.com/?p=113</guid>
		<description><![CDATA[One of the pieces of Game Trivia Catechism that I've been putting off for some time is the timer displayed on the question screen.  The timer is basically a reverse progress indicator.  It starts out full (with all of the lights on) and slowly counts down to empty (all lights off).  The simplest method I [...]]]></description>
			<content:encoded><![CDATA[<p>One of the pieces of Game Trivia Catechism that I've been putting off for some time is the timer displayed on the question screen.  The timer is basically a reverse progress indicator.  It starts out full (with all of the lights on) and slowly counts down to empty (all lights off).  The simplest method I know of for building a progress indicator is to use two images (one full, one empty) and simply crop the "full" to an appropriate sized based on the current progress position.</p>
<p>When it came time to implement this in GTC, I scoured the Cocoa Touch documentation for an easy way to crop a UIImage down to a specific size.  Apparently this was something that didn't make the cut.  Fortunately the lower level CoreGraphics methods provide a fairly simple method of doing this.  Here's what I ended up with.</p>
<p><code> - (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect<br />
{<br />
   <span style="color: #008000;">//create a context to do our clipping in</span><br />
   UIGraphicsBeginImageContext(rect.size);<br />
   CGContextRef currentContext = UIGraphicsGetCurrentContext();</code></p>
<p><code><span style="color: #008000;">   </span><span style="color: #339966;"><span style="color: #008000;">//create a rect with the size we want to crop the image to<br />
   //the X and Y here are zero so we start at the beginning of our<br />
   //newly created context</span></span><br />
   CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);<br />
   CGContextClipToRect( currentContext, clippedRect);</code></p>
<p><code><span style="color: #339966;"><span style="color: #008000;">   //create a rect equivalent to the full size of the image<br />
   //offset the rect by the X and Y we want to start the crop<br />
   //from in order to cut off anything before them</span></span><br />
   CGRect drawRect = CGRectMake(rect.origin.x * -1,<br />
                                rect.origin.y * -1,<br />
                                imageToCrop.size.width,<br />
                                imageToCrop.size.height);</code></p>
<p><code>   <span style="color: #339966;"><span style="color: #008000;">//draw the image to our clipped context using our offset rect</span></span><br />
   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);</code></p>
<p><code>   <span style="color: #339966;"><span style="color: #008000;">//pull the image from our cropped context</span></span><br />
   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();</code></p>
<p><code>   <span style="color: #339966;"><span style="color: #008000;">//pop the context to get back to the default</span></span><br />
   UIGraphicsEndImageContext();</code></p>
<p><code>   <span style="color: #339966;"><span style="color: #008000;">//Note: this is autoreleased</span></span><br />
   return cropped;<br />
}</code></p>
<p> </p>
<p>Then call the code with something like:<br />
<code>- (void)drawRect:(CGRect)rect<br />
{<br />
  <span style="color: #008000;"> //draw the whole lights off image<br />
   //the on images will be drawn overtop</span><br />
   [lightsOffImage drawInRect:rect];</code></p>
<p><code><code>   <span style="color: #008000;">//if we don't have any lights on... no point in continuing</span><br />
   if( numberOfLightsOn &lt; 1 )<br />
     return;</code></code></p>
<p><code><code>   <span style="color: #008000;">//figure out the dimensions of numberOfLights on bulbs</span><br />
   CGSize croppedSize = CGSizeMake(LIGHT_WIDTH * numberOfLightsOn, LIGHT_HEIGHT);<br />
   CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);</code></code></p>
<p><code><code>   <span style="color: #008000;">//get the "on" bulbs by cropping the image</span><br />
   UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];</code></code></p>
<p><code><code>   //create a rect to draw the newly cropped on images to<br />
   CGRect lightsOnRect = CGRectMake(LIGHT_BULB_OFFSET_X,<br />
                                    LIGHT_BULB_OFFSET_Y,<br />
                                    croppedSize.width,<br />
                                    croppedSize.height);</code></code></p>
<p><code><code>   <span style="color: #008000;">//draw the "on" lights</span><br />
   [cropped drawInRect:lightsOnRect];</code></p>
<p><code> </code><code>   <span style="color: #008000;">//cropped is autoreleased so no need to worry about cleanup</span><br />
}</code></p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
