<?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>Sorted Bits &#187; Cocoa</title>
	<atom:link href="http://www.sortedbits.com/category/coding/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sortedbits.com</link>
	<description>objectiveC, C# and more interesting stuff</description>
	<lastBuildDate>Wed, 04 Jan 2012 10:11:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Async downloading of data</title>
		<link>http://www.sortedbits.com/async-downloading-of-data/</link>
		<comments>http://www.sortedbits.com/async-downloading-of-data/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 08:07:49 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.sortedbits.com/?p=1573</guid>
		<description><![CDATA[This code shows you HOW to achieve something and it is not intended as a copy-paste solution. My post about using a NSURLConnection in a different thread has been read a lot. I made this post almost 3 years ago and I would like to show you [...]]]></description>
			<content:encoded><![CDATA[<div class="warning">
<div class="msg-box-icon pngfix">This code shows you HOW to achieve something and it is not intended as a copy-paste solution.</div>
</div>
<p>My post about using a <a href="http://www.sortedbits.com/nsurlconnection-in-its-own-thread">NSURLConnection in a different thread</a> has been read a lot. I made this post almost 3 years ago and I would like to show you how I would solve it nowadays. In the last 3 years I have learned a lot and the cocoa-touch framework evolved too.</p>
<p>Between that post and now I have been trying out frameworks that support async operations and after using <a href="http://allseeing-i.com/ASIHTTPRequest/">ASIHTTPRequest</a> for a long time, it was time to move on (the project was discontinued). After trying out several more, I came across <a href="https://github.com/lukeredpath/LRResty">LRResty</a>, which is a nice framework designed with <a href="http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html">code-blocks</a> in mind.</p>
<p>Here is a small example of a piece of code, which is taken directly form the <a href="http://projects.lukeredpath.co.uk/resty/documentation.html">LRResty documentation page</a>:</p>
<pre class="syntax c">
[[LRResty client] get:@&quot;http://www.example.com&quot;
            withBlock:^(LRRestyResponse *response) {

    if(response.status == 200) {
      NSLog(@&quot;Successful response %@&quot;, [response asString]);
    }
}];
</pre>
<p>This piece of code fetches the content of an URL (www.example.com) and when the response comes back, it checks if the response is OK (status code 200). When it is, it does a simple NSLog.</p>
<p>Here you can see the power of code-blocks already. You define the way it handles a responses directly when you do the request, in my opinion this keeps code clean and simple.</p>
<p>Now we can take this a step further and see what happens if we launch this in a new thread. For this example I am taking a piece of code of an actual library I was writing some time ago, it involves getting some data through the GitHub API, waiting for the response and then executing a certain piece of code, while this can be a bit overwhelming at first, it sure beats writing dozens of extra methods for each response.</p>
<p>Because we are going the be launching the request from another thread, we have to take <a href="http://en.wikipedia.org/wiki/Race_condition">race conditions</a> into account, this was a bug that hit me when I was first using code-blocks and multi-threading.</p>
<p>I created a method which can be called with a single NSDictionary as parameter, containing code-blocks and other stuff which it needs to perform the request. Because the GitHub API needs authentication also, I will also show how that works. The dictionary contains 3 objects with the keys &#8216;success&#8217;, &#8216;failed&#8217; and &#8216;url&#8217;.</p>
<p>The objects linked to the &#8216;success&#8217; and &#8216;failed&#8217; keys are code-blocks, I will show the contents later. </p>
<pre class="syntax c">
// This method performs the actual request and can be called in a different thread.
- (void) startRequest:(NSDictionary*) parameters
{
// Take the URL out of the dictionary and store it in a NSString object.
	NSString* url = [parameters objectForKey:@&quot;url&quot;];

// Get both the 'success' and 'failed' code-blocks from the dictionary
	void(^success)(LRRestyResponse*) = [parameters objectForKey:@&quot;success&quot;];
	void(^failed)(LRRestyResponse*) = [parameters objectForKey:@&quot;failed&quot;];

// Check if there already is a client active in this class and if not, check if authentication is set.
// If not, just raise an exception for now.
	if (client == nil &amp;&amp; self.username == nil &amp;&amp; self.password == nil)
	{
		[NSException raise:@&quot;Authentication missing&quot; format:@&quot;Username and/or password not set.&quot;];
	}

// No client is predefined and should be instantiated with the right credentials.
	if (client == nil)
		self.client = [LRResty authenticatedClientWithUsername:username password:password];

	NSLog(@&quot;Requesting data from: %@&quot;, url);

	[client get:url withBlock:^(LRRestyResponse* response) {
// Grab the headers of the response
		NSDictionary* dict = response.headers;

// Check if the request was finished without errors, if there were no errors, execute our 'success' block.
// Otherwise execute our 'failed' block.
// The response is a parameter for our success and failed block.
		if ([[dict objectForKey:@&quot;Status&quot;] isEqualToString:@&quot;200 OK&quot;])
		{
			success(response);
		}
		else
		{
			failed(response);
		}
	}];
}
</pre>
<p>With this method I can perform any request <strong>I</strong> want using a different thread. I am not a 100% sure this covers it in your cases (it probably doesn&#8217;t).</p>
<p>Now, lets see the code which we use to actually call the <strong>startRequest</strong> method:</p>
<pre class="syntax c">
// This gets a page of gists using the GitHub API. When it is succesfully done it executes a supplied code-block which
// extracts the gists from the response and returns an array. When it fails it will execute a code-block
// which will show an error in the application.
- (void) getGistsForPage:(int) page success:(void(^)(NSArray*)) succesBlock failed:(void(^)(void)) failedBlock
{
// Prepare the URL for the request.
	NSString* gistsUrl = [NSString stringWithFormat:@&quot;%@%@&quot;, self.githubUrl, @&quot;/gists&quot;];

// Here I define the code to be executed when the request is completed successfully. As you can see
// it grabs an array from the response and executes the supplied 'successBlock' with that array
// as a parameter.
	void (^success)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		NSArray* array = [response.responseData arrayValue];
		succesBlock(array);
	};

// This is the code that will be executed when the request fails. It simply executes the
// supplied failedBlock code.
	void (^failed)(LRRestyResponse*) = ^(LRRestyResponse* response) {
		failedBlock();
	};

// Call the startRequest method, with a dictionary which contains our succesBlock, our failedBlock and the URL
// we created earlier in this method.
	[NSThread detachNewThreadSelector:@selector(startRequest:)
							 toTarget:self
						   withObject:[NSDictionary dictionaryWithObjectsAndKeys:
									   success, @&quot;success&quot;,
									   failed, @&quot;failed&quot;,
									   gistsUrl, @&quot;url&quot;,
									   nil]];
}
</pre>
<p>It&#8217;s pretty straightforward. Now just to be complete, I will post the code I use to call the <strong>getGistsForPage</strong> method too, so you get a complete picture:</p>
<pre class="syntax c">
// Instantiate our API class, containing the methods I described above
GitHubAPI* api = [[GitHubAPI alloc] initWithURL:@&quot;https://api.github.com&quot;];

// Make sure the username, password and delegate are set
api.password = @&quot;your_password&quot;;
api.username = @&quot;your_username&quot;;
api.delegate = self;

// Call the method, which uses the multi-threaded part.
[api getGistsForPage:0 success:^(NSArray* array) {
	NSLog(@&quot;Array count: %lu&quot;, [array count]);
} failed:^(void) {
}];
</pre>
<p>That&#8217;s it, there is nothing more to it. <strong>For me</strong> this works like a charm!</p>
<p>PS. You can still use a delegate with LRResty, instead of the code-blocks. You need to implement this method when using it this way:</p>
<pre class="syntax c">
- (void)restClient:(LRRestyClient *)client receivedResponse:(LRRestyResponse *)res;
{
  // do something with the response
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/async-downloading-of-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A circle progressbar</title>
		<link>http://www.sortedbits.com/a-circle-progressbar/</link>
		<comments>http://www.sortedbits.com/a-circle-progressbar/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 07:35:55 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Snippets]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1458</guid>
		<description><![CDATA[For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress. [...]]]></description>
			<content:encoded><![CDATA[<p>For a small application I wanted to write, I wanted to make a progressbar that is a circle instead of a normal horizontal bar. I wanted to press a button, which is just a normal image and then have a circle go around it, showing the progress.</p>
<p><a href="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" rel="wp-prettyPhoto[1458]"><img src="http://www.wim.me/wp-content/uploads/2011/11/SBCircleProgressBar.png" alt="" title="SBCircleProgressBar" width="218" height="184" class="alignnone size-full wp-image-1459" /></a><br />
Like you can see above in the image, it just looks awesome!</p>
<p>To achieve this, I followed a couple of simple steps:</p>
<ul>
<li>Create a cool button image, where you want the circle progressbar to appear in</li>
<li>Find out the center and radius of the circle progressbar, you can do this either by using an image editor or just guess some values and tweak them when they are not completely perfect.</li>
<li>Create a class subclassing a UIView, which has 2 properties : MaxValue (the value that is the maximum you want to use) and Progress (what is the current progress of the progressbar)</li>
<li>Put it all on another View and change the Progress property when the progress of your process changes</li>
</ul>
<div id="gist-1378588" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">//</span></div><div class='line' id='LC2'><span class="c1">//  SBCircleProgressBar.h</span></div><div class='line' id='LC3'><span class="c1">//</span></div><div class='line' id='LC4'><span class="c1">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="c1">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="c1">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="k">@interface</span> <span class="nc">SBCircleProgressBar</span> : <span class="nc">UIView</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c1">// The maximum value of the progressbar</span></div><div class='line' id='LC11'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">MaxValue</span><span class="p">;</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c1">// The current value of the progressbar</span></div><div class='line' id='LC14'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">)</span> <span class="kt">float</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="k">@end</span></div><div class='line' id='LC17'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/02f81ee6ecdb257e2d68313ff611cfe1a21132d9/SBCircleProgressBar.h" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.h" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.h</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">//</span></div><div class='line' id='LC2'><span class="cp">//  SBCircleProgressBar.m</span></div><div class='line' id='LC3'><span class="cp">//</span></div><div class='line' id='LC4'><span class="cp">//  Created by Wim Haanstra on 17-11-11.</span></div><div class='line' id='LC5'><span class="cp">//  Copyright (c) 2011 Sorted Bits. All rights reserved.</span></div><div class='line' id='LC6'><span class="cp">//</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="cp">#import &quot;SBCircleProgressBar.h&quot;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="k">@implementation</span> <span class="nc">SBCircleProgressBar</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="k">@synthesize</span> <span class="n">MaxValue</span><span class="p">,</span> <span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="cp">// SOME VALUES YOU CAN CHANGE TO CHANGE THE BEHAVIOUR OF THE PROGRESSBAR</span></div><div class='line' id='LC15'><span class="cp">#define MAX_DEGREES	360</span></div><div class='line' id='LC16'><span class="cp">#define START_DEGREES	270</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="cp">// Define the radius that the circle needs</span></div><div class='line' id='LC19'><span class="cp">#define CIRCLE_RADIUS	27</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'><span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">initWithFrame:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="nv">frame</span></div><div class='line' id='LC22'><span class="p">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">super</span> <span class="nl">initWithFrame:</span><span class="n">frame</span><span class="p">];</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="n">self</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC25'><br/></div><div class='line' id='LC26'>		<span class="n">self</span><span class="p">.</span><span class="n">backgroundColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="n">clearColor</span><span class="p">];</span></div><div class='line' id='LC27'>		<span class="n">self</span><span class="p">.</span><span class="n">userInteractionEnabled</span> <span class="o">=</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>		<span class="c1">// Default values for the progress bar</span></div><div class='line' id='LC30'>		<span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span> <span class="o">=</span> <span class="mf">100.0f</span><span class="p">;</span></div><div class='line' id='LC31'>		<span class="n">self</span><span class="p">.</span><span class="n">Progress</span> <span class="o">=</span> <span class="mf">0.0f</span><span class="p">;</span></div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">self</span><span class="p">;</span></div><div class='line' id='LC35'><span class="p">}</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">setMaxValue:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_MaxValue</span></div><div class='line' id='LC38'><span class="p">{</span></div><div class='line' id='LC39'>	<span class="n">MaxValue</span> <span class="o">=</span> <span class="n">_MaxValue</span><span class="p">;</span></div><div class='line' id='LC40'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC41'><span class="p">}</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nl">setProgress:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">_Progress</span></div><div class='line' id='LC44'><span class="p">{</span></div><div class='line' id='LC45'>	<span class="n">Progress</span> <span class="o">=</span> <span class="n">_Progress</span><span class="p">;</span></div><div class='line' id='LC46'>	<span class="p">[</span><span class="n">self</span> <span class="n">setNeedsDisplay</span><span class="p">];</span></div><div class='line' id='LC47'><span class="p">}</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'><span class="o">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nl">drawRect:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="n">rect</span></div><div class='line' id='LC50'><span class="p">{</span></div><div class='line' id='LC51'>	<span class="n">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="n">UIGraphicsGetCurrentContext</span><span class="p">();</span> </div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>	<span class="c1">// Set the color of the circle to appear for the progressbar</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetRGBStrokeColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">0.341</span><span class="p">,</span> <span class="mf">0.635</span><span class="p">,</span> <span class="mf">0.961</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">);</span> </div><div class='line' id='LC55'><br/></div><div class='line' id='LC56'>	<span class="c1">// Set the line width of the circle to appear</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextSetLineWidth</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">);</span> </div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>	<span class="c1">// Calculate the middle of the circle</span></div><div class='line' id='LC60'>	<span class="n">CGPoint</span> <span class="n">circleCenter</span> <span class="o">=</span> <span class="n">CGPointMake</span><span class="p">(</span><span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">self</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC61'><br/></div><div class='line' id='LC62'>	<span class="c1">// Calculate the amount of degrees the circle needs to have filled</span></div><div class='line' id='LC63'>	<span class="kt">float</span> <span class="n">currentDegrees</span> <span class="o">=</span> <span class="p">(</span><span class="n">MAX_DEGREES</span> <span class="o">/</span> <span class="n">self</span><span class="p">.</span><span class="n">MaxValue</span><span class="p">)</span> <span class="o">*</span> <span class="n">self</span><span class="p">.</span><span class="n">Progress</span><span class="p">;</span></div><div class='line' id='LC64'><br/></div><div class='line' id='LC65'>	<span class="c1">// Draw the ARC (part of the circle</span></div><div class='line' id='LC66'>	<span class="n">CGContextAddArc</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">x</span> <span class="p">,</span> <span class="n">circleCenter</span><span class="p">.</span><span class="n">y</span><span class="p">,</span> <span class="n">CIRCLE_RADIUS</span><span class="p">,</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span><span class="p">),</span> <span class="n">radians</span><span class="p">(</span><span class="n">START_DEGREES</span> <span class="o">+</span> <span class="n">currentDegrees</span><span class="p">),</span> <span class="mi">0</span><span class="p">);</span> </div><div class='line' id='LC67'>	<span class="n">CGContextStrokePath</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC68'><span class="p">}</span></div><div class='line' id='LC69'><br/></div><div class='line' id='LC70'><br/></div><div class='line' id='LC71'><span class="k">@end</span></div><div class='line' id='LC72'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1378588/d61583a8c37ed11fac3905e5eb6adee8050c1e11/SBCircleProgressBar.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1378588#file_sb_circle_progress_bar.m" style="float:right;margin-right:10px;color:#666">SBCircleProgressBar.m</a>
            <a href="https://gist.github.com/1378588">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>You need to put this view OVER the image of your button. Because we do this, we also set userInteractionEnabled otherwise you wouldn&#8217;t be able to push your button anymore, because the progressbar would catch all the touches.</p>
<p>The method &#8220;radians&#8221; you see in there, is a small inline method used to calculate degrees to radians:
</p>
<pre class="syntax c">
static inline float radians(double degrees) { return degrees * M_PI / 180; }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/a-circle-progressbar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the interface orientations from your project definition</title>
		<link>http://www.sortedbits.com/interfaceorientations/</link>
		<comments>http://www.sortedbits.com/interfaceorientations/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 06:25:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1379</guid>
		<description><![CDATA[I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by [...]]]></description>
			<content:encoded><![CDATA[<p>I have always been annoyed by the way you have to handle your interface orientation detection and which orientations you allow for your application. You can specify the orientations in your project&#8217;s plist file, but I am not completely sure why the UIViewControllers don&#8217;t use this by default.</p>
<p>So for that purpose I made a method a long time ago, which I use on most UIViewControllers, except when they allow different interface orientations then default. I use this method as a static on some class I have, which helps me do pretty default stuff.</p>
<div id="gist-1302088" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">+</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span> <span class="nf">shouldAllowRotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span> <span class="n">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;&quot;</span><span class="p">;</span></div><div class='line' id='LC4'>	<span class="k">switch</span> <span class="p">(</span><span class="n">interfaceOrientation</span><span class="p">)</span></div><div class='line' id='LC5'>	<span class="p">{</span></div><div class='line' id='LC6'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeLeft:</span></div><div class='line' id='LC7'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeLeft&quot;</span><span class="p">;</span></div><div class='line' id='LC8'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC9'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationLandscapeRight:</span></div><div class='line' id='LC10'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationLandscapeRight&quot;</span><span class="p">;</span></div><div class='line' id='LC11'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC12'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortrait:</span></div><div class='line' id='LC13'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortrait&quot;</span><span class="p">;</span></div><div class='line' id='LC14'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC15'>		<span class="k">case</span> <span class="nl">UIInterfaceOrientationPortraitUpsideDown:</span></div><div class='line' id='LC16'>			<span class="n">currentOrientation</span> <span class="o">=</span> <span class="s">@&quot;UIInterfaceOrientationPortraitUpsideDown&quot;</span><span class="p">;</span></div><div class='line' id='LC17'>			<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC18'>	<span class="p">}</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'>	<span class="n">NSString</span><span class="o">*</span> <span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations&quot;</span><span class="p">;</span></div><div class='line' id='LC21'>	<span class="k">if</span><span class="p">([</span><span class="n">UIDevice</span> <span class="n">currentDevice</span><span class="p">].</span><span class="n">userInterfaceIdiom</span> <span class="o">==</span> <span class="n">UIUserInterfaceIdiomPad</span><span class="p">)</span></div><div class='line' id='LC22'>		<span class="n">supportedOrientationsKey</span> <span class="o">=</span> <span class="s">@&quot;UISupportedInterfaceOrientations~ipad&quot;</span><span class="p">;</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>	<span class="n">NSArray</span><span class="o">*</span> <span class="n">allowedOrientations</span> <span class="o">=</span> <span class="p">[[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="n">infoDictionary</span><span class="p">]</span> <span class="nl">objectForKey:</span><span class="n">supportedOrientationsKey</span><span class="p">];</span></div><div class='line' id='LC25'>	<span class="k">for</span> <span class="p">(</span><span class="n">NSString</span><span class="o">*</span> <span class="n">allowedOrientation</span> <span class="k">in</span> <span class="n">allowedOrientations</span><span class="p">)</span></div><div class='line' id='LC26'>	<span class="p">{</span></div><div class='line' id='LC27'>		<span class="k">if</span> <span class="p">([</span><span class="n">allowedOrientation</span> <span class="nl">isEqualToString:</span><span class="n">currentOrientation</span><span class="p">])</span></div><div class='line' id='LC28'>			<span class="k">return</span> <span class="n">YES</span><span class="p">;</span></div><div class='line' id='LC29'>	<span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'>	<span class="k">return</span> <span class="n">NO</span><span class="p">;</span></div><div class='line' id='LC32'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302088/6e24eb511bcfab7ef6636aa8856d6e00180ed666/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302088#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302088">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And you can just use it like this:</p>
<div id="gist-1302091" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">shouldAutorotateToInterfaceOrientation:</span><span class="p">(</span><span class="n">UIInterfaceOrientation</span><span class="p">)</span><span class="nv">interfaceOrientation</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="k">return</span> <span class="p">[</span><span class="n">WHUtilities</span> <span class="nl">shouldAllowRotateToInterfaceOrientation:</span><span class="n">interfaceOrientation</span><span class="p">];</span></div><div class='line' id='LC4'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302091/61be6b27c4da8765646ea41f812308ae2854e425/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302091#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302091">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/interfaceorientations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protected: Localize your UIViews (the easy way)</title>
		<link>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/</link>
		<comments>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:21:35 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1357</guid>
		<description><![CDATA[There is no excerpt because this is a protected post.]]></description>
			<content:encoded><![CDATA[<form action="http://www.sortedbits.com/wp-pass.php" method="post">
<p>This post is password protected. To view it please enter your password below:</p>
<p><label for="pwbox-1357">Password:<br />
<input name="post_password" id="pwbox-1357" type="password" size="20" /></label><br />
<input type="submit" name="Submit" value="Submit" /></p></form>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/localize-your-uiviews-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NSLog only in a DEBUG version</title>
		<link>http://www.sortedbits.com/using-nslog-only-in-a-debug-version/</link>
		<comments>http://www.sortedbits.com/using-nslog-only-in-a-debug-version/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 06:59:05 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[nslog]]></category>
		<category><![CDATA[objectivec]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wim.me/?p=1289</guid>
		<description><![CDATA[I have been using this in a couple of projects. It is a method to only perform NSLog logging when you are using a DEBUG build. There are more than enough reasons why you don&#8217;t want to use NSLog in your release builds. First of all, its [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using this in a couple of projects. It is a method to only perform NSLog logging when you are using a DEBUG build. There are more than enough reasons why you don&#8217;t want to use NSLog in your release builds.</p>
<p>First of all, its all about performance. I noticed that when you use NSLog a lot it decreases performance significantly. Ofcourse you can solve this in more ways, but I found this the easiest one.</p>
<p><span id="more-1289"></span></p>
<h3>Setting the Preprocessor Macro</h3>
<p>First you need to set a <strong>Preprocessor macro</strong> for your debug build. You can do this by double clicking on your target application. The <strong>Target Info</strong> window will popup and you need to make sure that you choose the <strong>Debug</strong> configuration.</p>
<p>The just perform a search in that window for <strong>Preprocessor Macros</strong>. Once you found that, make sure you add the value <strong>DEBUG=1</strong>.</p>
<h3>Adding some code</h3>
<p>Now we are going to add some code to your applications <strong>prefix file</strong>. Just look through your applications&#8217; sources for a file called something like <strong>YourApplication_Prefix.pch</strong>. Once you find that, open up that file and use the following code:</p>
<pre class="syntax c">
#ifdef DEBUG
    #define NSLog(format, ...) NSLog(format, ## __VA_ARGS__)
#else
    #define NSLog(format, ...)
#endif
</pre>
<p>This code overrides the normal NSLog functionality. And as you can see it will only execute a real NSLog when DEBUG is not set (so, your release build).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/using-nslog-only-in-a-debug-version/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Placing curved text on an UIImage</title>
		<link>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/</link>
		<comments>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 10:26:51 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[cocoa-touch]]></category>
		<category><![CDATA[curve]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objectivec]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=1210</guid>
		<description><![CDATA[For a project I am doing for a client, I needed to layout text around a circle in objective-C. I spend a couple of hours searching the internet for this, but somehow when people succeed in doing this, they don&#8217;t post their solution online. So, I made [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I am doing for a client, I needed to layout text around a circle in objective-C. I spend a couple of hours searching the internet for this, but somehow when people succeed in doing this, they don&#8217;t post their solution online.</p>
<p>So, I made an array of NSString objects that I wanted to lay out, around that circle.</p>
<div id="gist-1302242" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">NSArray</span><span class="o">*</span> <span class="n">sections</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSArray</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithObjects:</span><span class="s">@&quot;text1&quot;</span><span class="p">,</span> <span class="s">@&quot;text2&quot;</span><span class="p">,</span> <span class="s">@&quot;text3&quot;</span><span class="p">,</span> <span class="s">@&quot;text4&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">];</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="k">-</span> <span class="p">(</span><span class="n">UIImage</span><span class="o">*</span><span class="p">)</span> <span class="nf">createMenuRingWithFrame:</span><span class="p">(</span><span class="n">CGRect</span><span class="p">)</span><span class="nv">frame</span></div><div class='line' id='LC4'><span class="p">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// First fix the frame to make sure it uses the right scaling (iPhone 4 / iPad compatibility).</span></div><div class='line' id='LC6'>	<span class="n">frame</span> <span class="o">=</span> <span class="n">CGRectMake</span><span class="p">(</span><span class="n">frame</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">origin</span><span class="p">.</span><span class="n">y</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">*</span> <span class="n">scale</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">*</span> <span class="n">scale</span><span class="p">);</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Same for the text radius</span></div><div class='line' id='LC9'>	<span class="kt">float</span> <span class="n">scaledTextRadius</span> <span class="o">=</span> <span class="n">textRadius</span> <span class="o">*</span> <span class="n">scale</span><span class="p">;</span></div><div class='line' id='LC10'>	<span class="kt">float</span> <span class="n">scaledRingWidth</span> <span class="o">=</span> <span class="n">ringWidth</span><span class="p">;</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Define the centerpoint of the circle.</span></div><div class='line' id='LC13'>	<span class="n">CGPoint</span> <span class="n">centerPoint</span> <span class="o">=</span> <span class="n">CGPointMake</span><span class="p">(</span><span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">/</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// self.menuItemsFont is of the type UIFont, which I use to get the font for displaying the text on the circle.</span></div><div class='line' id='LC16'>	<span class="kt">char</span><span class="o">*</span> <span class="n">fontName</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)[</span><span class="n">self</span><span class="p">.</span><span class="n">menuItemsFont</span><span class="p">.</span><span class="n">fontName</span> <span class="nl">cStringUsingEncoding:</span><span class="n">NSASCIIStringEncoding</span><span class="p">];</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// 2 UIColor&#39;s, which I need for the color of the circle and for the text</span></div><div class='line' id='LC19'>	<span class="n">ringColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mi">1</span> <span class="nl">green:</span><span class="mi">1</span> <span class="nl">blue:</span><span class="mi">1</span> <span class="nl">alpha:</span><span class="mf">0.7</span><span class="p">];</span></div><div class='line' id='LC20'>	<span class="n">textColor</span> <span class="o">=</span> <span class="p">[</span><span class="n">UIColor</span> <span class="nl">colorWithRed:</span><span class="mi">0</span> <span class="nl">green:</span><span class="mi">0</span> <span class="nl">blue:</span><span class="mi">0</span> <span class="nl">alpha:</span><span class="mi">1</span><span class="p">];</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>	<span class="n">CGFloat</span><span class="o">*</span> <span class="n">ringColorComponents</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="o">*</span><span class="p">)</span><span class="n">CGColorGetComponents</span><span class="p">(</span><span class="n">ringColor</span><span class="p">.</span><span class="n">CGColor</span><span class="p">);</span></div><div class='line' id='LC23'>	<span class="n">CGFloat</span><span class="o">*</span> <span class="n">textColorComponents</span> <span class="o">=</span> <span class="p">(</span><span class="kt">float</span><span class="o">*</span><span class="p">)</span><span class="n">CGColorGetComponents</span><span class="p">(</span><span class="n">textColor</span><span class="p">.</span><span class="n">CGColor</span><span class="p">);</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>	<span class="n">CGColorSpaceRef</span> <span class="n">colorSpace</span> <span class="o">=</span> <span class="n">CGColorSpaceCreateDeviceRGB</span><span class="p">();</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">CGContextRef</span> <span class="n">context</span> <span class="o">=</span> <span class="n">CGBitmapContextCreate</span><span class="p">(</span><span class="nb">NULL</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span><span class="p">,</span> <span class="n">colorSpace</span><span class="p">,</span> <span class="n">kCGImageAlphaPremultipliedFirst</span><span class="p">);</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'>	<span class="n">CGContextSetTextMatrix</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">CGAffineTransformIdentity</span><span class="p">);</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>	<span class="n">CGContextSelectFont</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">fontName</span><span class="p">,</span> <span class="mi">18</span> <span class="o">*</span> <span class="n">scale</span><span class="p">,</span> <span class="n">kCGEncodingMacRoman</span><span class="p">);</span></div><div class='line' id='LC31'>	<span class="n">CGContextSetRGBStrokeColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">ringColorComponents</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">ringAlpha</span><span class="p">);</span></div><div class='line' id='LC32'>	<span class="n">CGContextSetLineWidth</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">scaledRingWidth</span><span class="p">);</span></div><div class='line' id='LC33'><br/></div><div class='line' id='LC34'>	<span class="n">CGContextStrokeEllipseInRect</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">CGRectMake</span><span class="p">(</span><span class="n">scaledRingWidth</span><span class="p">,</span> <span class="n">scaledRingWidth</span><span class="p">,</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span> <span class="o">-</span> <span class="p">(</span><span class="n">scaledRingWidth</span> <span class="o">*</span> <span class="mi">2</span><span class="p">),</span> <span class="n">frame</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span> <span class="o">-</span> <span class="p">(</span><span class="n">scaledRingWidth</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)));</span></div><div class='line' id='LC35'>	<span class="n">CGContextSetRGBFillColor</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">textColorComponents</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">textAlpha</span><span class="p">);</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'>	<span class="n">CGContextSaveGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC38'>	<span class="n">CGContextTranslateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">centerPoint</span><span class="p">.</span><span class="n">x</span><span class="p">,</span> <span class="n">centerPoint</span><span class="p">.</span><span class="n">y</span><span class="p">);</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// define the angles of the texts to divide them evenly on the circle.</span></div><div class='line' id='LC41'>	<span class="kt">float</span> <span class="n">angleStep</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">/</span> <span class="p">[</span><span class="n">sections</span> <span class="n">count</span><span class="p">];</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// I set the starting-angle on 90 degrees, to make sure the first text was ON TOP of the circle.</span></div><div class='line' id='LC44'>	<span class="kt">float</span> <span class="n">angle</span> <span class="o">=</span> <span class="n">degreesToRadians</span><span class="p">(</span><span class="mi">90</span><span class="p">);</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Some custom text-radius fixing (and scaling for iPhone 4 &amp;amp; iPad). To make sure my text centered nicely.</span></div><div class='line' id='LC47'>	<span class="n">scaledTextRadius</span> <span class="o">=</span> <span class="n">scaledTextRadius</span> <span class="o">-</span> <span class="mi">12</span> <span class="o">*</span> <span class="n">scale</span><span class="p">;</span></div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Loop through texts and draw them at the appropriate angle.</span></div><div class='line' id='LC50'>	<span class="k">for</span> <span class="p">(</span><span class="n">NSString</span><span class="o">*</span> <span class="n">text</span> <span class="k">in</span> <span class="n">sections</span><span class="p">)</span></div><div class='line' id='LC51'>	<span class="p">{</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// call a method that actually draws the text at an angle</span></div><div class='line' id='LC53'>		<span class="p">[</span><span class="n">self</span> <span class="nl">drawStringAtContext:</span><span class="n">context</span> <span class="nl">string:</span><span class="n">text</span> <span class="nl">atAngle:</span><span class="n">angle</span> <span class="nl">withRadius:</span><span class="n">scaledTextRadius</span><span class="p">];</span></div><div class='line' id='LC54'>		<span class="n">angle</span> <span class="o">-=</span> <span class="n">angleStep</span><span class="p">;</span></div><div class='line' id='LC55'>	<span class="p">}</span></div><div class='line' id='LC56'><br/></div><div class='line' id='LC57'>	<span class="n">CGContextRestoreGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>	<span class="n">CGImageRef</span> <span class="n">contextImage</span> <span class="o">=</span> <span class="n">CGBitmapContextCreateImage</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Release the stuff to avoid memory leaks.</span></div><div class='line' id='LC62'>	<span class="n">CGContextRelease</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC63'>	<span class="n">CGColorSpaceRelease</span><span class="p">(</span><span class="n">colorSpace</span><span class="p">);</span></div><div class='line' id='LC64'><br/></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="p">[</span><span class="n">UIImage</span> <span class="nl">imageWithCGImage:</span><span class="n">contextImage</span><span class="p">];</span></div><div class='line' id='LC66'><br/></div><div class='line' id='LC67'><span class="p">}</span></div><div class='line' id='LC68'><br/></div><div class='line' id='LC69'><span class="c1">// This method draws the text at an angle, while the location is based on the radius + angle.</span></div><div class='line' id='LC70'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span> <span class="nf">drawStringAtContext:</span><span class="p">(</span><span class="n">CGContextRef</span><span class="p">)</span> <span class="n">context</span> <span class="nl">string:</span><span class="p">(</span><span class="n">NSString</span><span class="o">*</span><span class="p">)</span> <span class="n">text</span> <span class="nl">atAngle:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">angle</span> <span class="nl">withRadius:</span><span class="p">(</span><span class="kt">float</span><span class="p">)</span> <span class="n">radius</span></div><div class='line' id='LC71'><span class="p">{</span></div><div class='line' id='LC72'>	<span class="c1">// Scale the fontsize to match UIScreen scaling</span></div><div class='line' id='LC73'>	<span class="n">UIFont</span><span class="o">*</span> <span class="n">scaledMenuItemsFont</span> <span class="o">=</span> <span class="p">[</span><span class="n">menuItemsFont</span> <span class="nl">fontWithSize:</span><span class="mi">18</span> <span class="o">*</span> <span class="n">scale</span><span class="p">];</span></div><div class='line' id='LC74'><br/></div><div class='line' id='LC75'>	<span class="c1">// Get the size of the string when it would be drawn with the selected font</span></div><div class='line' id='LC76'>	<span class="n">CGSize</span> <span class="n">textSize</span> <span class="o">=</span> <span class="p">[</span><span class="n">text</span> <span class="nl">sizeWithFont:</span><span class="n">scaledMenuItemsFont</span><span class="p">];</span></div><div class='line' id='LC77'><br/></div><div class='line' id='LC78'>	<span class="kt">float</span> <span class="n">perimeter</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span> <span class="o">*</span> <span class="n">radius</span><span class="p">;</span></div><div class='line' id='LC79'><br/></div><div class='line' id='LC80'>	<span class="c1">// determine the angle of the text.</span></div><div class='line' id='LC81'>	<span class="kt">float</span> <span class="n">textAngle</span> <span class="o">=</span> <span class="p">(</span><span class="n">textSize</span><span class="p">.</span><span class="n">width</span><span class="p">)</span> <span class="o">/</span> <span class="n">perimeter</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">;</span></div><div class='line' id='LC82'>	<span class="n">angle</span> <span class="o">+=</span> <span class="n">textAngle</span> <span class="o">/</span> <span class="mi">2</span><span class="p">;</span></div><div class='line' id='LC83'><br/></div><div class='line' id='LC84'>	<span class="c1">// We loop through each letter in the string, so we can set the angle for each letter right</span></div><div class='line' id='LC85'>	<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">index</span> <span class="o">&amp;</span><span class="n">lt</span><span class="p">;</span> <span class="p">[</span><span class="n">text</span> <span class="n">length</span><span class="p">];</span> <span class="n">index</span><span class="o">++</span><span class="p">)</span></div><div class='line' id='LC86'>	<span class="p">{</span></div><div class='line' id='LC87'>	    <span class="c1">// Get the correct letter by the index</span></div><div class='line' id='LC88'>		<span class="n">NSRange</span> <span class="n">range</span> <span class="o">=</span> <span class="p">{</span><span class="n">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">};</span></div><div class='line' id='LC89'>		<span class="n">NSString</span><span class="o">*</span> <span class="n">letter</span> <span class="o">=</span> <span class="p">[</span><span class="n">text</span> <span class="nl">substringWithRange:</span><span class="n">range</span><span class="p">];</span></div><div class='line' id='LC90'>		<span class="kt">char</span><span class="o">*</span> <span class="n">c</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)[</span><span class="n">letter</span> <span class="nl">cStringUsingEncoding:</span><span class="n">NSASCIIStringEncoding</span><span class="p">];</span></div><div class='line' id='LC91'>		<span class="n">CGSize</span> <span class="n">charSize</span> <span class="o">=</span> <span class="p">[</span><span class="n">letter</span> <span class="nl">sizeWithFont:</span><span class="n">scaledMenuItemsFont</span><span class="p">];</span></div><div class='line' id='LC92'><br/></div><div class='line' id='LC93'>	    <span class="c1">// Determin the X and Y position of the letter based on the angle and the radius</span></div><div class='line' id='LC94'>		<span class="kt">float</span> <span class="n">x</span> <span class="o">=</span> <span class="n">radius</span> <span class="o">*</span> <span class="n">cos</span><span class="p">(</span><span class="n">angle</span><span class="p">);</span></div><div class='line' id='LC95'>		<span class="kt">float</span> <span class="n">y</span> <span class="o">=</span> <span class="n">radius</span> <span class="o">*</span> <span class="n">sin</span><span class="p">(</span><span class="n">angle</span><span class="p">);</span></div><div class='line' id='LC96'><br/></div><div class='line' id='LC97'>		<span class="kt">float</span> <span class="n">letterAngle</span> <span class="o">=</span> <span class="p">(</span><span class="n">charSize</span><span class="p">.</span><span class="n">width</span> <span class="o">/</span> <span class="n">perimeter</span> <span class="o">*</span> <span class="o">-</span><span class="mi">2</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">);</span></div><div class='line' id='LC98'><br/></div><div class='line' id='LC99'>	    <span class="c1">// Save the state of the context, because rotations are based on the original origin</span></div><div class='line' id='LC100'>		<span class="n">CGContextSaveGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC101'>		<span class="n">CGContextTranslateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">);</span></div><div class='line' id='LC102'>	    <span class="c1">// Rotate the canvas so we can just draw our text horizontally</span></div><div class='line' id='LC103'>		<span class="n">CGContextRotateCTM</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="p">(</span><span class="n">angle</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">M_PI</span><span class="p">));</span></div><div class='line' id='LC104'>		<span class="n">CGContextShowTextAtPoint</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">strlen</span><span class="p">(</span><span class="n">c</span><span class="p">));</span></div><div class='line' id='LC105'>	    <span class="c1">// Restore the context again.</span></div><div class='line' id='LC106'>		<span class="n">CGContextRestoreGState</span><span class="p">(</span><span class="n">context</span><span class="p">);</span></div><div class='line' id='LC107'><br/></div><div class='line' id='LC108'>		<span class="n">angle</span> <span class="o">+=</span> <span class="n">letterAngle</span><span class="p">;</span></div><div class='line' id='LC109'>	<span class="p">}</span></div><div class='line' id='LC110'><span class="p">}</span></div><div class='line' id='LC111'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1302242/0d62efc44eb7d0b2219731bc7a424fd41d4e3103/gistfile1.m" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1302242#file_gistfile1.m" style="float:right;margin-right:10px;color:#666">gistfile1.m</a>
            <a href="https://gist.github.com/1302242">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I came to this solution after I asked a question on StackOverflow, which was helpfully demonstrated by Dribbel on how to do it. Credits for that to him.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/placing-curved-text-on-an-uiimage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Localization of your iOS application</title>
		<link>http://www.sortedbits.com/localization-of-your-io-app/</link>
		<comments>http://www.sortedbits.com/localization-of-your-io-app/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 21:48:56 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=1108</guid>
		<description><![CDATA[For a client I am working on an application that should be available in multiple languages. Well I always created my applications with English in mind, so this was a new one for me. I started this project in Xcode {REDACTED} and I could not get it [...]]]></description>
			<content:encoded><![CDATA[<p>For a client I am working on an application that should be available in multiple languages. Well I always created my applications with English in mind, so this was a new one for me. I started this project in Xcode {REDACTED} and I could not get it to work somehow. After an half hour or so, I got tired of running my app, switching languages, and running it again, that I tried switching back to Xcode 3.2.4.</p>
<p><span id="more-1108"></span><br />
Here are the steps I took to get localization working for my iPhone app:</p>
<h4><strong>Created a new application</strong></h4>
<p>I created a new application, just to be sure that my target settings wouldn&#8217;t be all screwed up by me testing everything.</p>
<h4><strong>Created language specific directories</strong></h4>
<p>In my folder, where my project was located, I created 3 directories. For each language 1:</p>
<ul>
<li><strong>en.lproj</strong> : This is where the English localization files go</li>
<li><strong>nl.lproj</strong> : This is where the Dutch localizations files go</li>
<li><strong>de.lproj</strong> : This is where the German localization files go</li>
</ul>
<h4><strong>Created strings files</strong></h4>
<p>In each of those directories I created 2 <strong>strings</strong> files, I did this by right-clicking the resources folder in Xcode, select <strong>Add</strong> -&gt; <strong>New File</strong>. Then click the <strong>Resource</strong> tag under <strong>Mac OS X</strong> and select <strong>Strings file</strong>.</p>
<p>When the <strong>New File</strong> dialog comes up, make sure you select the right location. You want to create 2 files <strong>per</strong> language directory you created with the step mentioned above. Name the files:</p>
<ul>
<li><strong>InfoPlist.strings</strong> : This file I created to be able to localize my application name</li>
<li><strong>Localizable.strings</strong> : This file contains all the text my application is showing, in the appropriate language</li>
</ul>
<p><span style="color: #ff0000">Mind the capitalization, this is very important because we are running on a case sensitive file system.</span></p>
<h4><strong>Fill your Localizable.string file</strong></h4>
<p>What you need to do now is maybe a little bit of hassle, but you need to copy all the string you used in your application to this application and give them keys by which you can identify them. With keys I mean <strong>unique keys</strong>.</p>
<p>For example, my localization file in the directory <strong>nl.lproj</strong> looked a bit like this:</p>
<pre class="syntax c">&quot;Add new product&quot; = &quot;Voeg product toe&quot;;</pre>
<p>While my localization file in the <strong>de.lproj</strong> looked like this:</p>
<pre class="syntax c">&quot;Add new product&quot; = &quot;Neue produkt&quot;;</pre>
<p>I think you get the point.</p>
<h4><strong>Actually display the strings</strong></h4>
<p>Now, after you spend hours filling your localization files and translating it to every language known to man you want to actually display some text in your application. Well that is pretty easy.</p>
<pre class="syntax c">- (void) doSomeStuff
{
	myLabel.text = NSLocalizedString(@&quot;Add new product&quot;, @&quot;&quot;);
}</pre>
<p>Whenever a language file for the users language could not be found, it will default back to the <strong>key</strong> and show that instead. So make sure your keys also say what you want your button/label/etc to say. That&#8217;s it! There isn&#8217;t more to it than that.</p>
<h4><strong>But what about my application name?</strong></h4>
<p>I almost forgot. Remember that you created 2 files for each language? Well, just put this line in your InfoPlist.strings files (ofcourse with the translated application names).</p>
<pre class="syntax c">CFBundleDisplayName = &quot;My App Name&quot;;</pre>
<p>Hope this helps you out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/localization-of-your-io-app/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Putting a UIPickerView on a UIActionSheet</title>
		<link>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/</link>
		<comments>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:24:50 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[uiactionsheet]]></category>
		<category><![CDATA[uipickerview]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=1002</guid>
		<description><![CDATA[Basically there are no really good (and now working) guides on how to put a UIPickerView on a UIActionSheet. Mainly because some things changed with the latest firmware updates made by Apple. Well after some fiddling around, I got it working. Here is how: Step 1: Adding [...]]]></description>
			<content:encoded><![CDATA[<p>Basically there are no really good (and now working) guides on how to put a UIPickerView on a UIActionSheet. Mainly because some things changed with the latest firmware updates made by Apple. Well after some fiddling around, I got it working. Here is how:</p>
<p><strong>Step 1: Adding the right delegates</strong><br />
Make sure your current class (probably a ViewController) uses the following delegates:</p>
<ul>
<li>UIActionSheetDelegate</li>
<li>UINavigationControllerDelegate</li>
<li>UIPickerViewDelegate</li>
<li>UIPickerViewDataSource</li>
</ul>
<p><span id="more-1002"></span></p>
<p><strong>Step 2: Trigger the UIActionSheet</strong><br />
Go to the method where you want to make the UIPickerView appear. In my case it is when I select a row from a table, but of course it could also be when you push a button or anything you want.</p>
<pre class="syntax c">/* first create a UIActionSheet, where you define a title, delegate and a button to close the sheet again */
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@&quot;Select position&quot; delegate:self cancelButtonTitle:@&quot;Done&quot; destructiveButtonTitle:nil otherButtonTitles:nil];
/* I always give my controls a tag, to make sure I can work with multiple actionsheets in one View (to identify them when an event triggers */
actionSheet.tag = POSITION_ROW;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

/* Initialize a UIPickerView with 100px space above it, for the button of the UIActionSheet. */
UIPickerView* positionPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100, 320, 216)];
positionPicker.dataSource = self;
positionPicker.delegate = self;

/* another unique tag for this UIPicker */
positionPicker.tag = POSITION_ROW;

/* Add the UIPickerView to the UIActionSheet */
[actionSheet addSubview:positionPicker];

/* Select the previous selected value, which for me is stored in 'currentPosition' */
[positionPicker selectRow:currentPosition inComponent:0 animated:NO];

/* clean up */
[positionPicker release];

/* Add the UIActionSheet to the view */
[actionSheet showInView:self.view];

/* Make sure the UIActionSheet is big enough to fit your UIPickerView and it's buttons */
[actionSheet setBounds:CGRectMake(0,0, 320, 411)];

/* clean up */
[actionSheet release];</pre>
<p><strong>Step 3: Implementing the datasource and delegates for the UIPickerView</strong><br />
Now, the triggering of the UIActionSheet is now working, but you would also like to have some items in your UIPickerView. Well, take a look at the following code and read the comments.</p>
<pre class="syntax c">/* Defines the total number of Components (like groups in a UITableView) in a UIPickerView */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

/* What to do when a row from a UIPickerView is selected. This will trigger each time you scroll the UIPickerView, so only lightweight stuff. */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	overlayPosition = row;
	[tableSettings reloadData];
}

/* For me the number of items in the UIPickerView is known on compile time. So here I just return 3 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return 3;
}

/* Because the UIPickerView expects a UIView for every row you insert in the UIPickerView, you need to make one. What I do here is really simple. I create a UILabel* and with each row it requests I just change the text in the UILabel. */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
/* Create a UILabel and set it 40 pixels to the right, to make sure it is put nicely in the UIPickerView */
	UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 280, 25)] autorelease];
	label.textColor = [UIColor blackColor];
	label.backgroundColor = [UIColor clearColor];

	switch (row)
	{
		default:
		case kUnderClock:
			label.text = @&quot;Under clock&quot;;
			break;
		case kAboveSlider:
			label.text = @&quot;Above lock slider&quot;;
			break;
		case kCenter:
			label.text = @&quot;Vertically centered&quot;;
			break;
	}

	return label;
}</pre>
<p><strong>Step 4: Do stuff when buttons on the UIActionSheet are clicked</strong><br />
Now, the final thing you have to do is make sure you listen to the events of the UIActionSheet also. So here is the code:</p>
<pre class="syntax c">- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (actionSheet.tag == POSITION_ROW)
	{
/* Do stuff here, only intended for the right UIActionSheet. This only applies for when you use multiple UIActionSheets on the same ViewController. Closing it is unnecessary, because you already specified a destructive button. */
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/putting-a-uipickerview-on-a-uiactionsheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending an email with an attachment from your iPhone</title>
		<link>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/</link>
		<comments>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 10:29:48 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[desktop backgrounds hd]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=840</guid>
		<description><![CDATA[For Desktop Backgrounds HD (which is currently in review) I wanted to implement a feature called &#8220;send image by email&#8221;. First I tried to include an image base64 encoded in an URL, with the mailto: protocol, but this resulted in numerous errors. So after a coupe of [...]]]></description>
			<content:encoded><![CDATA[<p>For Desktop Backgrounds HD (which is currently in review) I wanted to implement a feature called &#8220;send image by email&#8221;. First I tried to include an image base64 encoded in an URL, with the mailto: protocol, but this resulted in numerous errors.</p>
<p>So after a coupe of google searches, I came up with the following solution, follow these steps and you will be sending emails with attachments before you can say &#8220;emails with attachments&#8221; 250 times.<br />
<span id="more-851"></span><br />
<strong>Step 1</strong><br />
First add the MessageUI framework in your application.</p>
<p><strong>Step 2</strong><br />
The class you want to use the functionality in, should get the following 2 imports:</p>
<pre>#import
#import </pre>
<p><strong>Step 3</strong><br />
Make sure your class listens to the MFMailComposeViewControllerDelegate.</p>
<p><strong>Step 4</strong><br />
Next up, is to add code to add an attachment to the mail composer and open it up.</p>
<pre>MFMailComposeViewController* composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;

[composer setSubject:@"The subject of the email"];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"test.png"], 1);
[composer addAttachmentData:imageData mimeType:@"image/png" fileName:@"test.png"];
NSString *emailBody = @"Check out this amazing PNG!";
[composer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];</pre>
<p><strong>Step 5</strong><br />
Add this method, to make sure you get back full control over your view-controller:</p>
<pre>- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
	[self dismissModalViewControllerAnimated:YES];
}</pre>
<p>In this last method you can also perform error checking, which is not included in this example. Hope this helps anyone, and if not&#8230; too bad <img src='http://www.sortedbits.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/sending-an-email-with-an-attachment-from-your-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Save an UIView to an UIImage in objective-C</title>
		<link>http://www.sortedbits.com/save-an-uiview-to-an-uiimage-in-objective-c/</link>
		<comments>http://www.sortedbits.com/save-an-uiview-to-an-uiimage-in-objective-c/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 14:14:55 +0000</pubDate>
		<dc:creator>Wim</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[desktop backgrounds hd]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[uiimage]]></category>
		<category><![CDATA[uiview]]></category>

		<guid isPermaLink="false">http://www.wimhaanstra.com/?p=837</guid>
		<description><![CDATA[For an application I am making, I am making a view where users can configure all kinds of settings, which has direct effect on an UIView I have. I want to grab that UIView and save it as an image. Well there are examples enough about this, [...]]]></description>
			<content:encoded><![CDATA[<p>For an application I am making, I am making a view where users can configure all kinds of settings, which has direct effect on an UIView I have. I want to grab that UIView and save it as an image.</p>
<p>Well there are examples enough about this, most of them are all the same, but there is one annoying thing. It saves the view in the good ol&#8217; resolution. Because I am making all my apps &#8220;retina-aware&#8221;, I wanted to change this.</p>
<p><span id="more-850"></span>So here is my code snippet of how I save an UIView to an UIImage:</p>
<pre>UIGraphicsBeginImageContextWithOptions(myView.bounds.size, NO, [[UIScreen mainScreen] scale]);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();</pre>
<p>It automaticly takes care of any scaling, if there will be higher resolution iPhones/iPads in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sortedbits.com/save-an-uiview-to-an-uiimage-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

