<?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>[iPhoneiOS dev:tips]; &#187; Graphics</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/graphics/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Crop an Image</title>
		<link>http://iPhoneDeveloperTips.com/graphics/how-to-crop-an-image.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/how-to-crop-an-image.html#comments</comments>
		<pubDate>Fri, 04 Jun 2010 02:44:29 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6306</guid>
		<description><![CDATA[This post shows an example of one way to crop an image. Let&#8217;s begin by looking at a screenshot of the original and cropped image on the iPhone simulator: As you can see, I am cropping a rectangle from the middle of the image on the top left. The code below shows how to accomplished [...]]]></description>
			<content:encoded><![CDATA[<p>This post shows an example of one way to crop an image. Let&#8217;s begin by looking at a screenshot of the original and cropped image on the iPhone simulator:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/06/crop.png" /></p>
<p>As you can see, I am cropping a rectangle from the middle of the image on the top left. The code below shows how to accomplished this:<br />
<span id="more-6306"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create the image from a png file</span>
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;prgBinary.jpg&quot;</span><span style="color: #002200;">&#93;</span>;
UIImageView <span style="color: #002200;">*</span>imageView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get size of current image</span>
CGSize size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>image size<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Frame location in view to show original image</span>
<span style="color: #002200;">&#91;</span>imageView setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>imageView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>imageView release<span style="color: #002200;">&#93;</span>;	
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create rectangle that represents a cropped image  </span>
<span style="color: #11740a; font-style: italic;">// from the middle of the existing image</span>
CGRect rect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">4</span>, size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">4</span> , 
    <span style="color: #002200;">&#40;</span>size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span>size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create bitmap image from original image data,</span>
<span style="color: #11740a; font-style: italic;">// using rectangle to specify desired crop area</span>
CGImageRef imageRef <span style="color: #002200;">=</span> CGImageCreateWithImageInRect<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>image CGImage<span style="color: #002200;">&#93;</span>, rect<span style="color: #002200;">&#41;</span>;
UIImage <span style="color: #002200;">*</span>img <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>imageRef<span style="color: #002200;">&#93;</span>; 
CGImageRelease<span style="color: #002200;">&#40;</span>imageRef<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create and show the new image from bitmap data</span>
imageView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span>img<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>imageView setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">200</span>, <span style="color: #002200;">&#40;</span>size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span>size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>imageView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>imageView release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>All the heavy lifting is done by the method <strong>CGImageCreateWithImageInRect</strong>, which creates a new bitmap image using existing image data and a rectangle that is a subregion of that same image. Once cropped, I create a new <strong>UIImageView</strong> and add it as a subview of the current view.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/how-to-crop-an-image.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Convert an Image (UIImage) to Grayscale</title>
		<link>http://iPhoneDeveloperTips.com/graphics/convert-an-image-uiimage-to-grayscale.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/convert-an-image-uiimage-to-grayscale.html#comments</comments>
		<pubDate>Thu, 13 May 2010 11:07:44 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6249</guid>
		<description><![CDATA[A trick you won&#8217;t need often, yet when the day arrives, you&#8217;ll have this up your sleeve &#8211; a quick method to convert a UIImage object to grayscale. - &#40;UIImage *&#41;convertImageToGrayScale:&#40;UIImage *&#41;image &#123; // Create image rectangle with current image width/height CGRect imageRect = CGRectMake&#40;0, 0, image.size.width, image.size.height&#41;; &#160; // Grayscale color space CGColorSpaceRef colorSpace [...]]]></description>
			<content:encoded><![CDATA[<p>A trick you won&#8217;t need often, yet when the day arrives, you&#8217;ll have this up your sleeve &#8211; a quick method to convert a UIImage object to grayscale.<br />
<span id="more-6249"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>convertImageToGrayScale<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create image rectangle with current image width/height</span>
  CGRect imageRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, image.size.width, image.size.height<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Grayscale color space</span>
  CGColorSpaceRef colorSpace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceGray<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create bitmap content with current image size and grayscale colorspace</span>
  CGContextRef context <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span><span style="color: #a61390;">nil</span>, image.size.width, image.size.height, <span style="color: #2400d9;">8</span>, <span style="color: #2400d9;">0</span>, colorSpace, kCGImageAlphaNone<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Draw image into current context, with specified rectangle</span>
  <span style="color: #11740a; font-style: italic;">// using previously defined context (with grayscale colorspace)</span>
  CGContextDrawImage<span style="color: #002200;">&#40;</span>context, imageRect, <span style="color: #002200;">&#91;</span>image CGImage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create bitmap image info from pixel data in current context</span>
  CGImageRef imageRef <span style="color: #002200;">=</span> CGBitmapContextCreateImage<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create a new UIImage object  </span>
  UIImage <span style="color: #002200;">*</span>newImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>imageRef<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Release colorspace, context and bitmap information</span>
  CGColorSpaceRelease<span style="color: #002200;">&#40;</span>colorSpace<span style="color: #002200;">&#41;</span>;
  CGContextRelease<span style="color: #002200;">&#40;</span>context<span style="color: #002200;">&#41;</span>;
  CFRelease<span style="color: #002200;">&#40;</span>imageRef<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Return the new grayscale image</span>
  <span style="color: #a61390;">return</span> newImage;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Simple enough  &#8211; here is an example of a converted image:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/05/grayscale.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/convert-an-image-uiimage-to-grayscale.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resize/Scale of an Image &#8211; Take 2 &#8211; Coding a Thread Safe Approach</title>
		<link>http://iPhoneDeveloperTips.com/graphics/how-to-resize-scale-an-image-thread-safe-take-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/how-to-resize-scale-an-image-thread-safe-take-2.html#comments</comments>
		<pubDate>Mon, 25 Jan 2010 14:37:07 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5339</guid>
		<description><![CDATA[In the first post on image resizing, How to Resize/Scale an Image using an Objective-C Category, I wrote barebones approach to resizing an image. This works well for simple cases, however this approach is not thread safe as it uses the global current context. I attended a recent Apple Tech Talk and one of the [...]]]></description>
			<content:encoded><![CDATA[<p>In the first post on image resizing, <a  target="_blank"  href="http://iphonedevelopertips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html">How to Resize/Scale an Image using an Objective-C Category</a>, I wrote barebones approach to resizing an image. This works well for simple cases, however this approach is not thread safe as it uses the global current context.</p>
<p>I attended a recent Apple Tech Talk and one of the more interesting discussions was on how to create code to dynamically resize images, using an approach that is thread safe. An experienced developer at the event was willing to share an excellent code example that I&#8217;ll walk through in this post. The code is a fair amount more complex than the first version I wrote, however, with the complexity comes flexibility.<br />
<span id="more-5339"></span></p>
<h5>Building the User Interface</h5>
<p>The project to demonstrate thread-safe image resizing is quite simple, essentially a table and a slider, with the later controlling the size of the images in the table. The image below should give you and idea of how things work:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/img1.png" alt="" title="img1" width="450" height="331" class="alignnone size-full wp-image-5341" /></p>
<p>It all starts by building a <strong>UITableView</strong> and setting the <strong>imageView</strong> property to one of the images in the application bundle. The code to build (or reload) the table looks as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView 
   cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath
<span style="color: #002200;">&#123;</span>
  UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> 
	<span style="color: #002200;">&#40;</span>UITableViewCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>kMyCellID<span style="color: #002200;">&#93;</span>;
  ...
&nbsp;
  cell.imageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self requestImageForIndex<span style="color: #002200;">:</span>row<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The relevant line of code is 8, notice the call to <strong>requestImageForIndex</strong>, the code for this method is shown below. There is a fair amount going on here, first an <strong>ImageStateObject</strong> is created, this keeps track of the path of the image, a flag indicating if the image has been loaded and a flag indicating if a (resize) operation is in progress. There is also code for managing a queue of operations -to keep focuses on the task at hand, skip down to LINE 20, where we call <strong>UImageFromPathScaledToSize</strong> passing in the image state object and the preferred size.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> requestImageForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>index
<span style="color: #002200;">&#123;</span>
  ImageStateObject <span style="color: #002200;">*</span>iso	<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self imageObjectForIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
  CGSize  theSz <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self preferredImageSize<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> mUseOperations <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> iso.hasImage <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span> <span style="color: #002200;">&amp;&amp;</span> iso.queuedOp <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span> <span style="color: #002200;">&#41;</span>	<span style="color: #11740a; font-style: italic;">// if we dont have an image and there is no operation pending</span>
    <span style="color: #002200;">&#123;</span>	
      <span style="color: #11740a; font-style: italic;">// Queue up an operation to do the work!</span>
      MyLoadScaleOperation <span style="color: #002200;">*</span>op <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyLoadScaleOperation alloc<span style="color: #002200;">&#93;</span> initWithPath<span style="color: #002200;">:</span>iso.path index<span style="color: #002200;">:</span>index targetSize<span style="color: #002200;">:</span>theSz<span style="color: #002200;">&#93;</span>;
      op.resultsDelegate <span style="color: #002200;">=</span> self;		<span style="color: #11740a; font-style: italic;">// set the delegate</span>
      <span style="color: #002200;">&#91;</span>mQueue addOperation<span style="color: #002200;">:</span>op<span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span>op release<span style="color: #002200;">&#93;</span>;
&nbsp;
      iso.queuedOp <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #002200;">&#125;</span>		
    <span style="color: #a61390;">return</span> mPlaceHolderImage;		<span style="color: #11740a; font-style: italic;">// just return our placeholder</span>
  <span style="color: #002200;">&#125;</span>	
  <span style="color: #a61390;">return</span> UImageFromPathScaledToSize<span style="color: #002200;">&#40;</span> iso.path, theSz <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note that when calling the method on line 20, notice the path to the image is passed in as the first parameter.</p>
<h5>ImageStateobject</h5>
<p>For completeness, here is the definition of the image state object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> ImageStateObject <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path;
  <span style="color: #a61390;">BOOL</span>  hasImage;
  <span style="color: #a61390;">BOOL</span>  queuedOp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code for working with the queued operations (<strong>queuedOp</strong>) we will look into in a future post.</p>
<h5>UImageFromPathScaledToSize</h5>
<p>Given a path to an image and a specified size, create a <strong>UIImage</strong> object and get the scale value based on the current image size and the size to scale to, and finish by creating a new <strong>UIImage</strong> object from the original image, scaled as requested.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>UImageFromPathScaledToSize<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> path, CGSize toSize<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  UIImage <span style="color: #002200;">*</span>scaledImg <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
  UIImage <span style="color: #002200;">*</span>img <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;	<span style="color: #11740a; font-style: italic;">// get the image</span>
&nbsp;
  <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> img <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">float</span> scale <span style="color: #002200;">=</span> GetScaleForProportionalResize<span style="color: #002200;">&#40;</span> img.size, toSize, <span style="color: #a61390;">false</span>, <span style="color: #a61390;">false</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
    CGImageRef cgImage	<span style="color: #002200;">=</span> CreateCGImageFromUIImageScaled<span style="color: #002200;">&#40;</span> img, scale <span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>img release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> cgImage <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      scaledImg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>cgImage<span style="color: #002200;">&#93;</span>;	<span style="color: #11740a; font-style: italic;">// autoreleased</span>
      CGImageRelease<span style="color: #002200;">&#40;</span> cgImage <span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> scaledImg;	<span style="color: #11740a; font-style: italic;">// autoreleased</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Get Scale Percentage and Create New Image Reference</h5>
<p>Below is the code to determine the scale value as a percentage. For example, if the incoming image size (<strong>theSize</strong>) is 266 x 401 and the destination size (<strong>intoSize</strong>) is 225 x 225, the returned scale value is .561 (225/401). That is, we want to scale the image to 56% of its current size.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">float</span> GetScaleForProportionalResize<span style="color: #002200;">&#40;</span> CGSize theSize, CGSize intoSize, bool onlyScaleDown, bool maximize <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">float</span> sx <span style="color: #002200;">=</span> theSize.width;
  <span style="color: #a61390;">float</span> sy <span style="color: #002200;">=</span> theSize.height;
  <span style="color: #a61390;">float</span> dx <span style="color: #002200;">=</span> intoSize.width;
  <span style="color: #a61390;">float</span> dy <span style="color: #002200;">=</span> intoSize.height;
  <span style="color: #a61390;">float</span> scale <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
&nbsp;
  <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sx <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&amp;&amp;</span> sy <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    dx <span style="color: #002200;">=</span> dx <span style="color: #002200;">/</span> sx;
    dy	 <span style="color: #002200;">=</span> dy <span style="color: #002200;">/</span> sy;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// if maximize is true, take LARGER of the scales, else smaller</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> maximize <span style="color: #002200;">&#41;</span>
      scale <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>dx &gt; dy<span style="color: #002200;">&#41;</span>	? dx <span style="color: #002200;">:</span> dy;
    <span style="color: #a61390;">else</span>			
      scale <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>dx &lt; dy<span style="color: #002200;">&#41;</span>	? dx <span style="color: #002200;">:</span> dy;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> scale &gt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&amp;&amp;</span> onlyScaleDown <span style="color: #002200;">&#41;</span>	<span style="color: #11740a; font-style: italic;">// reset scale</span>
      scale <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>
    scale <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> scale;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The last step is to create a <strong>CGImageRef</strong> which will hold bitmap information for the new, scaled image. This reference will then be used to create our final scaled <strong>UIImage</strong> object.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">CGContextRef CreateCGBitmapContextForWidthAndHeight<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> width, <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> height, 
    CGColorSpaceRef optionalColorSpace, CGBitmapInfo optionalInfo <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  CGColorSpaceRef colorSpace <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>optionalColorSpace <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> ? GetDeviceRGBColorSpace<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">:</span> optionalColorSpace;
  CGBitmapInfo alphaInfo	<span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>int32_t<span style="color: #002200;">&#41;</span>optionalInfo &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> ? kDefaultCGBitmapInfo <span style="color: #002200;">:</span> optionalInfo;
  <span style="color: #a61390;">return</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">NULL</span>, width, height, <span style="color: #2400d9;">8</span>, <span style="color: #2400d9;">0</span>, colorSpace, alphaInfo <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
CGImageRef CreateCGImageFromUIImageScaled<span style="color: #002200;">&#40;</span> UIImage<span style="color: #002200;">*</span> image, <span style="color: #a61390;">float</span> scaleFactor <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  CGImageRef newImage <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
  CGContextRef bmContext <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
  <span style="color: #a61390;">BOOL</span>  mustTransform <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
  CGAffineTransform  transform <span style="color: #002200;">=</span> CGAffineTransformIdentity;
  UIImageOrientation orientation <span style="color: #002200;">=</span> image.imageOrientation;
&nbsp;
  CGImageRef srcCGImage <span style="color: #002200;">=</span> CGImageRetain<span style="color: #002200;">&#40;</span> image.CGImage <span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">size_t</span> width <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>srcCGImage<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> scaleFactor;
  <span style="color: #a61390;">size_t</span> height <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>srcCGImage<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> scaleFactor;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// These Orientations are rotated 0 or 180 degrees, so they retain the width/height of the image</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>orientation <span style="color: #002200;">==</span> UIImageOrientationUp<span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>orientation <span style="color: #002200;">==</span> UIImageOrientationDown<span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>orientation <span style="color: #002200;">==</span> UIImageOrientationUpMirrored<span style="color: #002200;">&#41;</span> || <span style="color: #002200;">&#40;</span>orientation <span style="color: #002200;">==</span> UIImageOrientationDownMirrored<span style="color: #002200;">&#41;</span>  <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>	
    bmContext	<span style="color: #002200;">=</span> CreateCGBitmapContextForWidthAndHeight<span style="color: #002200;">&#40;</span> width, height, <span style="color: #a61390;">NULL</span>, kDefaultCGBitmapInfo <span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>	<span style="color: #11740a; font-style: italic;">// The other Orientations are rotated ±90 degrees, so they swap width &amp; height.</span>
  <span style="color: #002200;">&#123;</span>	
    bmContext	<span style="color: #002200;">=</span> CreateCGBitmapContextForWidthAndHeight<span style="color: #002200;">&#40;</span> height, width, <span style="color: #a61390;">NULL</span>, kDefaultCGBitmapInfo <span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  CGContextSetBlendMode<span style="color: #002200;">&#40;</span> bmContext, kCGBlendModeCopy <span style="color: #002200;">&#41;</span>;	<span style="color: #11740a; font-style: italic;">// we just want to copy the data</span>
&nbsp;
  <span style="color: #a61390;">switch</span><span style="color: #002200;">&#40;</span>orientation<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">case</span> UIImageOrientationDown<span style="color: #002200;">:</span>		<span style="color: #11740a; font-style: italic;">// 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, width, height<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationLeft<span style="color: #002200;">:</span>		<span style="color: #11740a; font-style: italic;">// 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, height, <span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationRight<span style="color: #002200;">:</span>		<span style="color: #11740a; font-style: italic;">// 0th row is on the right, and 0th column is the top - Rotate 90 degrees</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, width<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationUpMirrored<span style="color: #002200;">:</span>	<span style="color: #11740a; font-style: italic;">// 0th row is at the top, and 0th column is on the right - Flip Horizontal</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span>, width, <span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationDownMirrored<span style="color: #002200;">:</span>	<span style="color: #11740a; font-style: italic;">// 0th row is at the bottom, and 0th column is on the left - Flip Vertical</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, height<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationLeftMirrored<span style="color: #002200;">:</span>	<span style="color: #11740a; font-style: italic;">// 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, height, width<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">case</span> UIImageOrientationRightMirrored<span style="color: #002200;">:</span>	<span style="color: #11740a; font-style: italic;">// 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical</span>
    transform <span style="color: #002200;">=</span> CGAffineTransformMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">break</span>;
&nbsp;
    <span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
    mustTransform <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #a61390;">break</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> mustTransform <span style="color: #002200;">&#41;</span>	
    CGContextConcatCTM<span style="color: #002200;">&#40;</span> bmContext, transform <span style="color: #002200;">&#41;</span>;
&nbsp;
  CGContextDrawImage<span style="color: #002200;">&#40;</span> bmContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, width, height<span style="color: #002200;">&#41;</span>, srcCGImage <span style="color: #002200;">&#41;</span>;
  CGImageRelease<span style="color: #002200;">&#40;</span> srcCGImage <span style="color: #002200;">&#41;</span>;
  newImage <span style="color: #002200;">=</span> CGBitmapContextCreateImage<span style="color: #002200;">&#40;</span> bmContext <span style="color: #002200;">&#41;</span>;
  CFRelease<span style="color: #002200;">&#40;</span> bmContext <span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> newImage;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Project Source Code</h5>
<p>You can download the entire project <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/NSOperationTest.zip">source code here</a>. You&#8217;ll find the image processing code shown above is in the source file ImageHelpers.m. </p>
<p>There are many other worthwhile code snippets to look at in this project, including code to create and display gradients (for the table background) as well as working with <strong>NSOperation</strong> and <strong>NSOperationQueue</strong> to place resize requests into a queue for processing.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/how-to-resize-scale-an-image-thread-safe-take-2.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Gotcha: Case Sensitive PNG Filename</title>
		<link>http://iPhoneDeveloperTips.com/graphics/gotcha-case-sensitive-png-filename.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/gotcha-case-sensitive-png-filename.html#comments</comments>
		<pubDate>Thu, 10 Dec 2009 08:07:07 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4830</guid>
		<description><![CDATA[Here&#8217;s one that will trip you up if you aren&#8217;t careful&#8230;look at this code: timerButton = &#91;&#91;UIButton alloc&#93; initWithFrame:CGRectMake&#40;104, 410, 50, 50&#41;&#93;; &#160; // Notice the filename &#34;timer.png&#34; &#91;timerButton setBackgroundImage:&#91;UIImage imageNamed:@&#34;timer.png&#34;&#93; forState:UIControlStateNormal&#93;; &#160; &#91;timerButton addTarget:self action:@selector&#40;buttonPressed:&#41; forControlEvents: UIControlEventTouchUpInside&#93;; &#160; &#91;self.view addSubview:timerButton&#93;; Simple enough, create a button, use the image timer.png for the normal state, and [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s one that will trip you up if you aren&#8217;t careful&#8230;look at this code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">timerButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">104</span>, <span style="color: #2400d9;">410</span>, <span style="color: #2400d9;">50</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
&nbsp;
<span style="color: #11740a; font-style: italic;">// Notice the filename &quot;timer.png&quot;</span>
<span style="color: #002200;">&#91;</span>timerButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;timer.png&quot;</span><span style="color: #002200;">&#93;</span> 
    forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>timerButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
   forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>timerButton<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Simple enough, create a button, use the image <strong>timer.png</strong> for the normal state, and all is well&#8230;if you run this code in the simulator. Problem is, if the actual filename of the image is <strong>Timer.png</strong> the png file will not appear when running this code on device.</p>
<p>If you haven&#8217;t run into this while developing an iPhone app, you can&#8217;t fully appreciate how time consuming it can be to track this down.</p>
<p><strong>Note:</strong> I ran into this while working with Xcode 3.2.1 and building for iPhone OS 3.1.2</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/gotcha-case-sensitive-png-filename.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Resize/Scale of an Image &#8211; Take 1 &#8211; Using an Objective-C Category</title>
		<link>http://iPhoneDeveloperTips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html#comments</comments>
		<pubDate>Wed, 11 Nov 2009 08:52:18 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4576</guid>
		<description><![CDATA[Editor&#8217;s Note: Part 2 of this post Resize/Scale of an Image &#8211; Take 2 &#8211; Thread Safe Approach takes this idea to the next level, showing how to dynamically resize images using thread safe code. Given how common it is to scale an image within an application, it&#8217;s surprising (at least to me) that such [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Editor&#8217;s Note</strong>: Part 2 of this post <a  target="_blank"  href="http://iphonedevelopertips.com/graphics/how-to-resize-scale-an-image-thread-safe-take-2.html">Resize/Scale of an Image &#8211; Take 2 &#8211; Thread Safe Approach</a> takes this idea to the next level, showing how to dynamically resize images using thread safe code.</em></p>
<p>Given how common it is to scale an image within an application, it&#8217;s surprising (at least to me) that such a method is not included in the <strong>UIImage</strong> class. Let&#8217;s go ahead and take care of this omission by adding a simple method which will provide a means to scale an image.</p>
<p>We&#8217;ll use an Objective-C category to add a method to the <strong>UIImage</strong> class. I covered <a  target="_blank"  href="http://iphonedevelopertips.com/objective-c/categories.html">categories in this post</a>. No question, categories are your friend, as they are very handy in cases such as this, where you would like to add functionality to an existing class.<br />
<span id="more-4576"></span></p>
<h5>Defining the UIImage Scale Category</h5>
<p>To add a method to the <strong>UIImage</strong> class, there are two steps we need to take care in the interface defintion. First, create an interface definition for <strong>UIImage</strong> and add a category name inside a set of parenthesis after the interface declaration. Second, create the method declaration. An example follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UIImage+Scale.h</span>
&nbsp;
<span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>scale<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>scaleToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Implementation UIImage Scale Category</h5>
<p>With the interface in place, let&#8217;s write the code for the method that will be added to the <strong>UIImage</strong> class.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  UIImage+Scale.h</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;UIImage+Scale.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>scale<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>scaleToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create a bitmap graphics context</span>
  <span style="color: #11740a; font-style: italic;">// This will also set it as the current context</span>
  UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>size<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Draw the scaled image in the current context</span>
  <span style="color: #002200;">&#91;</span>self drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create a new image from current context</span>
  UIImage<span style="color: #002200;">*</span> scaledImage <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Pop the current context from the stack</span>
  UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Return our new scaled image</span>
  <span style="color: #a61390;">return</span> scaledImage;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Using the UIImage Scale Method</h5>
<p>Calling the new scale method we added to <strong>UIImage</strong> is as simple as this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;UIImage+Scale.h&quot;</span>
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create an image</span>
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;myImage.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Scale the image</span>
UIImage <span style="color: #002200;">*</span>scaledImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>image scaleToSize<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span>25.0f, 35.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/how-to-scale-an-image-using-an-objective-c-category.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Animated Gif &#8211; Animated Images &#8211; iPhone Style</title>
		<link>http://iPhoneDeveloperTips.com/graphics/animated-gif-animated-images-iphone-style.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/animated-gif-animated-images-iphone-style.html#comments</comments>
		<pubDate>Thu, 16 Jul 2009 15:01:24 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2594</guid>
		<description><![CDATA[Using animated gif and animated images with the iPhone SDK.]]></description>
			<content:encoded><![CDATA[<p>Ah, the animated gif, a throwback to the early days of web development and simple animation. In this post I&#8217;ll show you how to use a <strong>UIImageView</strong> to create a similar effect that you get with an animated gif.</p>
<p>Below I&#8217;ve inserted the animated gif that I&#8217;ll use as a template for the iPhone app we&#8217;ll write in this post. </p>
<p><img width="240" height="180" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/07/crad.gif" /><br />
<span id="more-2594"></span></p>
<p>Animated gifs are nothing more than a series of images, encapsulated within one gif file, that are displayed in sequence. Within an animated gif one can specify the duration that each image (aka frame) is displayed, which offers a wide range effects that you can achieve.</p>
<p><strong>Animated Images on the iPhone</strong><br />
To achieve a similar effect in an iPhone app, we create a <strong>UIImageView</strong> and set the <strong>animationImages</strong> property to point to an array of images. Not unlike the animated gif, we now have a series of images in which we can control the amount of time to complete one cycle of the images, as well as the repeat count of how many times to cycle through the animation.</p>
<p>To keep the example short, I&#8217;ll use only one class, the application delegate. Here is how I&#8217;ve defined the delegate class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> animationAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UIImageView   <span style="color: #002200;">*</span>animatedImages;
  <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>imageArray;
  UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Not much here, the <strong>UIImageView</strong>, an array to hold the images and the <strong>UIWindow</strong> for the output.</p>
<p>The code for the implementation of the app delegate follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;animationAppDelegate.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define IMAGE_COUNT       36</span>
<span style="color: #6e371a;">#define IMAGE_WIDTH       240</span>
<span style="color: #6e371a;">#define IMAGE_HEIGHT      180</span>
<span style="color: #6e371a;">#define STATUS_BAR_HEIGHT 20</span>
<span style="color: #6e371a;">#define SCREEN_HEIGHT     460</span>
<span style="color: #6e371a;">#define SCREEN_WIDTH      320</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> animationAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopAnimation
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>animatedImages stopAnimating<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application
<span style="color: #002200;">&#123;</span>
  window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Array to hold jpg images</span>
  imageArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span>IMAGE_COUNT<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Build array of images, cycling through image names</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; IMAGE_COUNT; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>imageArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>
              <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Frame_%d.jpg&quot;</span>, i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Animated images - centered on screen</span>
   animatedImages <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> 
     initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>
        <span style="color: #002200;">&#40;</span>SCREEN_WIDTH <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IMAGE_WIDTH <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, 
        <span style="color: #002200;">&#40;</span>SCREEN_HEIGHT <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IMAGE_HEIGHT <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> STATUS_BAR_HEIGHT,
        IMAGE_WIDTH, IMAGE_HEIGHT<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  animatedImages.animationImages <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithArray<span style="color: #002200;">:</span>imageArray<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// One cycle through all the images takes 1 seconds</span>
  animatedImages.animationDuration <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Repeat forever</span>
  animatedImages.animationRepeatCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add subview and make window visible</span>
  <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>animatedImages<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start it up</span>
  animatedImages.startAnimating;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Wait 5 seconds, then stop animation</span>
  <span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>stopAnimation<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> afterDelay<span style="color: #002200;">:</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>imageArray release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>animatedImages release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>window release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>How it Works</strong><br />
On lines 3 &#8211; 5 we define the attributes for the images in the animation. There are a total of 36 images, each is 240 x 180 pixels. On line 22 the array is defined which will hold the images and lines 25 &#8211; 27 the images are added to the array. You can decipher from the code that the images I am using have file names in the format: Frame_1.jpg, Frame_2.jpg, Frame_3.jpg, etc.</p>
<p>On lines 30 &#8211; 34 we allocate an <strong>UIImageView</strong> object and configure the frame so the image is centered on the device display. Line 35 sets the <strong>animationImages</strong> property to the array of images created above.</p>
<p>Line 38 sets the duration for one cycle through all the images. We specify the images are to cycle indefinitely by setting the animationRepeatCount to -1 on line 41.</p>
<p>At this point we are pretty much good to go, all we need to do is add the <strong>UIImageView</strong> object as a subview (line 44) and start the animation (line 48).</p>
<p><strong>Application in the iPhone Simulator</strong><br />
The movie below shows how the application looks when running in the iPhone simulator:</p>
<p> <embed src="http://iphonedevelopertips.com/wp-content/uploads/2009/07/crad.mov" width="300" height="450">  </p>
<p><strong>Porting Animated Gif to iPhone</strong><br />
Not unlike I did for the example above, if you have an animated gif that you would like to use within an iPhone app, it&#8217;s as simple as extracting/saving each image inside a gif and using a sequential naming convention. I opened the original gif file and manually saved each frame in the image to a jpg file. There are a number of tools that will do the extraction/saving/renaming for you (search for gif extractor). </p>
<p>With all the images saved as separate file, swap the image attributes in the #define section and update the code on line 27 to match the filename you used for saving each file.</p>
<p><strong>Xcode Project Source Code</strong><br />
Download the <a href="http://iphonedevelopertips.com/wp-content/uploads/2009/07/xcode_animation.zip">Animation Xcode project</a>.</p>
<table bgcolor="#f4f3e8" border="0">
<tr>
<td>&nbsp;&nbsp;Animated gif of Newtons cradle, courtesy: <a target="_blank" href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format">Wikipedia</a>&nbsp;&nbsp;</td>
</tr>
</table>
<p><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/animated-gif-animated-images-iphone-style.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2009/07/crad.mov" length="67459" type="video/quicktime" />
		</item>
		<item>
		<title>Creating iPhone Icons</title>
		<link>http://iPhoneDeveloperTips.com/graphics/creating-iphone-icons.html</link>
		<comments>http://iPhoneDeveloperTips.com/graphics/creating-iphone-icons.html#comments</comments>
		<pubDate>Thu, 18 Sep 2008 07:08:49 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[icon]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=488</guid>
		<description><![CDATA[I&#8217;ve been poking around for a decent tutorial on how to create icons for the iPhone. Specifically, I am looking for a walk through with either PhotoShop or FireWorks. The icon I have in mind is what will be shown on the home screen of the iPhone as well as on the App Store. The [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been poking around for a decent tutorial on how to create icons for the iPhone. Specifically, I am looking for a walk through with either PhotoShop or FireWorks. The icon I have in mind is what will be shown on the home screen of the iPhone as well as on the App Store.</p>
<p>The first tutorial I found that does a nice job of walking through the basics is from <a href="http://www.keepthewebweird.com/" target="_blank">KeepTheWebWeird</a>, you can view the tutorial <a href="http://www.keepthewebweird.com/iphone-style-icon-tutorial/" target="_blank">here.</a> A screenshot of the example created is shown below.<br />
<span id="more-488"></span></p>
<p><img src="http://blog.jotlet.net/images/final_product_picture.jpg" alt="" /></p>
<p>You can also view two movies that accompany the tutorial:</p>
<ol>
<li><a href="http://blog.jotlet.net/images/edgeglare.mov" target="_blank">The Edge Glare Step</a></li>
<li><a href="http://blog.jotlet.net/images/restoftutorial.mov" target="_blank">The Rest of the Stuff</a></li>
</ol>
<p>If you know of other good tutorials for working with icons, please drop me an email or post a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/graphics/creating-iphone-icons.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://blog.jotlet.net/images/edgeglare.mov" length="738866" type="video/quicktime" />
<enclosure url="http://blog.jotlet.net/images/restoftutorial.mov" length="7416531" type="video/quicktime" />
		</item>
	</channel>
</rss>
