<?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>[iPhone developer:tips];</title>
	<atom:link href="http://iPhoneDeveloperTips.com/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Mon, 08 Feb 2010 13:59:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sleep, Pause or Block a Thread</title>
		<link>http://iPhoneDeveloperTips.com/core-services/sleep-pause-or-block-a-thread.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/sleep-pause-or-block-a-thread.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 08:34:07 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5431</guid>
		<description><![CDATA[If you ever need to briefly pause a thread of execution, there are two class methods in NSThread you can use. Although you have to use these methods judiciously, they can be handy in instances where you need to force a delay in order to achieve some desired effect. 
For example, if you want to [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever need to briefly pause a thread of execution, there are two class methods in <strong>NSThread</strong> you can use. Although you have to use these methods judiciously, they can be handy in instances where you need to force a delay in order to achieve some desired effect. </p>
<p>For example, if you want to slowly fade-in (increase) the volume of the audio player over a few seconds, you could write a loop to bump the volume a small increment each time through the loop, and pause briefly before heading back to the top of the loop. If you didn&#8217;t include a short pause within the loop, the application may run the loop so quickly that you could not audibly detect the fade-in.</p>
<h5>sleepForTimeInterval</h5>
<p>One approach is to use the class method <strong>sleepForTimeInterval</strong> method:</p>
<p><strong>  + (void)sleepForTimeInterval:(NSTimeInterval)ti</strong></p>
<p>The variable <strong>NSTimeInterval</strong> is of type double and represents the number of seconds to sleep</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Block for .5 seconds</span>
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSThread</span> sleepForTimeInterval<span style="color: #002200;">:</span>.5<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5> sleepUntilDate </h5>
<p>Another approach is to use an <strong>NSDate</strong> object to specify a specific time in the future to resume the thread:</p>
<p><strong>+ (void)sleepUntilDate:(NSDate *)aDate</strong></p>
<p>With this method you pass in an NSDate object that specifies how long to block the thread.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Block for 2.5 seconds from &quot;now&quot;</span>
 <span style="color: #400080;">NSThread</span> sleepUntilDate<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> dateWithTimeIntervalSinceNow<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/sleep-pause-or-block-a-thread.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rotate an Image or Button with Animation &#8211; Part 2</title>
		<link>http://iPhoneDeveloperTips.com/user-interface/rotate-an-image-or-button-with-animation-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/user-interface/rotate-an-image-or-button-with-animation-part-2.html#comments</comments>
		<pubDate>Mon, 01 Feb 2010 08:07:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5399</guid>
		<description><![CDATA[In a previous post Rotate an Image with Animation &#8211; Part 1 I wrote a short code block using CGAffineTransform to rotate an image. Problem is, if you want to rotate an image 360 degrees, or repeat a rotation a specified number of times, you&#8217;ll need to take a different approach, which is the focus [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post <a  target="_blank"  href="http://iphonedevelopertips.com/user-interface/rotate-an-image-with-animation.html">Rotate an Image with Animation &#8211; Part 1</a> I wrote a short code block using <strong>CGAffineTransform</strong> to rotate an image. Problem is, if you want to rotate an image 360 degrees, or repeat a rotation a specified number of times, you&#8217;ll need to take a different approach, which is the focus of this post. This code example will use a layer for animation so this same effect can used on images, buttons, etc.</p>
<p>The video below shows a rotating a button (on the left) as well as image. Notice the direction of rotation is clockwise on the button and counter-clockwise on the image. Also, notice that the rate of the spin is different for each object.<br />
<span id="more-5399"></span></p>
<p><embed src="http://iphonedevelopertips.com/wp-content/uploads/2010/01/rotate.mov" width="332" height="160" autoplay="true"> </p>
<h5>Spin/Rotate Code</h5>
<p>This method accepts a layer object (<strong>CALayer</strong>), the duration of the spin (in seconds) and the direction:</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><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>spinLayer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CALayer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inLayer duration<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CFTimeInterval<span style="color: #002200;">&#41;</span>inDuration
     direction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>direction
<span style="color: #002200;">&#123;</span>
  CABasicAnimation<span style="color: #002200;">*</span> rotationAnimation;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Rotate about the z axis</span>
  rotationAnimation <span style="color: #002200;">=</span> 
    <span style="color: #002200;">&#91;</span>CABasicAnimation animationWithKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;transform.rotation.z&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Rotate 360 degress, in direction specified</span>
  rotationAnimation.toValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span> M_PI <span style="color: #002200;">*</span> <span style="color: #2400d9;">2.0</span> <span style="color: #002200;">*</span> direction<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Perform the rotation over this many seconds</span>
  rotationAnimation.duration <span style="color: #002200;">=</span> inDuration;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the pacing of the animation</span>
  rotationAnimation.timingFunction <span style="color: #002200;">=</span> 
    <span style="color: #002200;">&#91;</span>CAMediaTimingFunction functionWithName<span style="color: #002200;">:</span>kCAMediaTimingFunctionEaseInEaseOut<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add animation to the layer and make it so</span>
  <span style="color: #002200;">&#91;</span>inLayer addAnimation<span style="color: #002200;">:</span>rotationAnimation forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAnimation&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Calling the spinLayer Method</h5>
<p>The code below shows how to call the method, specifying the duration and the direction. Make note that the layer associated with the image/button is passed in to the method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define SPIN_CLOCK_WISE 1</span>
<span style="color: #6e371a;">#define SPIN_COUNTERCLOCK_WISE -1</span>
&nbsp;
UIImageView  <span style="color: #002200;">*</span>testImage;
...
<span style="color: #002200;">&#91;</span>self spinLayer<span style="color: #002200;">:</span>testImage.layer duration<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span> direction<span style="color: #002200;">:</span>SPIN_CLOCK_WISE<span style="color: #002200;">&#93;</span>;
&nbsp;
UIButton <span style="color: #002200;">*</span>infoButton;
...
<span style="color: #002200;">&#91;</span>self spinLayer<span style="color: #002200;">:</span>infoButton.layer duration<span style="color: #002200;">:</span><span style="color: #2400d9;">3</span> direction<span style="color: #002200;">:</span>SPIN_COUNTERCLOCK_WISE<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Spin Effect &#8211; Timing Functions</h5>
<p>Notice in the original example I specified <strong>kCAMediaTimingFunctionEaseInEaseOut</strong> as the timing function name, which defines the pacing of the animation, in this case, the animation begins slowly, accelerates and then slows again.</p>
<p>You can experiment with the following timing functions for various effects:</p>
<p>kCAMediaTimingFunctionLinear<br />
kCAMediaTimingFunctionEaseIn<br />
kCAMediaTimingFunctionEaseOut<br />
kCAMediaTimingFunctionEaseInEaseOut<br />
kCAMediaTimingFunctionDefault</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/user-interface/rotate-an-image-or-button-with-animation-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2010/01/rotate.mov" length="148547" type="video/quicktime" />
		</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 more [...]]]></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>1</slash:comments>
		</item>
		<item>
		<title>Create Readable Links to Your iPhone Apps and Company in the App Store</title>
		<link>http://iPhoneDeveloperTips.com/itunes/create-readable-links-to-your-iphone-apps-and-company-in-the-app-store.html</link>
		<comments>http://iPhoneDeveloperTips.com/itunes/create-readable-links-to-your-iphone-apps-and-company-in-the-app-store.html#comments</comments>
		<pubDate>Tue, 19 Jan 2010 03:20:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[iTunes]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5326</guid>
		<description><![CDATA[Creating a link to your application or company in the App Store serves many purposes, linking from your website, sending a link to a friend or app review website, etc. You can easily create a link to your application by right clicking on the application name or icon in iTunes. For example, referring to Cartoons [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a link to your application or company in the App Store serves many purposes, linking from your website, sending a link to a friend or app review website, etc. You can easily create a link to your application by right clicking on the application name or icon in iTunes. For example, referring to Cartoons to Go application, if you right-click on the application name you end up with this link: <strong>http://itunes.apple.com/us/app/cartoons-to-go/id305713868?mt=8</strong>.<br />
<span id="more-5326"></span></p>
<h5>Creating More Readable Link</h5>
<p>If you want to create a link that is easier to read, you can follow the format shown below, replacing your application name where indicated:</p>
<p>  <strong>http://itunes.com/apps/application_name_here</strong></p>
<p>For example, the equivalent link to the Cartoons To Go app new becomes: <strong>http://itunes.com/apps/cartoonstogo</strong></p>
<p>Notice that I removed all the spaces in the application name. Here is the complete list of naming requirements:</p>
<p>- Remove all whitespace<br />
- Remove the following characters and symbols: !¡&#8221;#$%&#8217;()*+,\-./:;<=>¿?@[\]^_`{|}~©™®<br />
- Replace ampersands with the word &#8220;and&#8221;<br />
- Accented characters such as ü should be replaced with base character (for example &#8220;u&#8221;)<br />
- Use only lower-case letters</p>
<p>The above rules apply to both linking to your application as well as the company name. For example to access the company name <strong>3 Sixty Software, LLC</strong> in the App Store, the readable link is: <strong>http://itunes.com/apps/3sixtysoftwarellc</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/itunes/create-readable-links-to-your-iphone-apps-and-company-in-the-app-store.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sam Kinison and Scream Scenes iPhone Apps</title>
		<link>http://iPhoneDeveloperTips.com/iphone-apps/sam-kinison-and-scream-scenes-iphone-apps.html</link>
		<comments>http://iPhoneDeveloperTips.com/iphone-apps/sam-kinison-and-scream-scenes-iphone-apps.html#comments</comments>
		<pubDate>Thu, 14 Jan 2010 15:03:58 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5286</guid>
		<description><![CDATA[Mill Creek Entertainment is in the business of providing video home entertainment. Their DVD product line consists of classic movies, television episodes, historical documentaries and feature films.
I&#8217;ve been working with Mill Creek to come up with creative ways to leverage their content in digital formats beyond DVD with the iPhone as the first mobile platform.
Scream [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://millcreekent.com/" target="_blank">Mill Creek Entertainment</a> is in the business of providing video home entertainment. Their DVD product line consists of classic movies, television episodes, historical documentaries and feature films.</p>
<p><a href="http://itunes.apple.com/us/app/scream-scenes-classic-horror-clips/id334405166?mt=8" target="_blank"><img class="alignleft size-medium wp-image-5289" style="margin: 5px;" title="scream" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/scream-206x300.png" alt="" width="206" height="300" /></a>I&#8217;ve been working with Mill Creek to come up with creative ways to leverage their content in digital formats beyond DVD with the iPhone as the first mobile platform.</p>
<p><a href="http://itunes.apple.com/us/app/scream-scenes-classic-horror-clips/id334405166?mt=8" target="_blank">Scream Scenes</a> was released in the fall of 2009 and featured audio and video clips from 16 of the most frighteningly fantastic classic horror films which can be found in Mill Creek Entertainment’s <a href="http://www.millcreekent.com/ViewProductDetails.asp?productid=21" target="_blank">Horror Classics 50 Movie MegaPack</a>.</p>
<p>If you tap on the any of the 16 movie icons, a short audio clip of a &#8220;classic scream&#8221; will play. If you touch and hold one of the icons, the video segment from the movie showing the &#8220;classic scream&#8221; will play.</p>
<h5>Sam Kinison</h5>
<p>Our second collaborative project was the <a href="http://itunes.apple.com/us/app/id344285959?mt=8" target="_blank">Sam Kinison Scream Board</a>, which follows the same idea of Scream Scenes, audio and video content from a collection of Sam Kinison DVDs. If you are not familiar with Sam Kinison, let&#8217;s just say his sense of humor can be a little shocking. If you are okay with Sam&#8217;s no holds barred approach (the app has a 17+ rating) his standup comedy makes for some great laughs.</p>
<p>The <a href="http://itunes.apple.com/us/app/id344285959?mt=8 " target="_blank">Sam Kinison app</a> is free, if you are up for some outrageous fun, give it a try.</p>
<p><a href="http://itunes.apple.com/us/app/id344285959?mt=8" target="_blank"><img class="alignleft size-full wp-image-5304" title="sam1" src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/sam1.png" alt="" width="450" height="323" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/iphone-apps/sam-kinison-and-scream-scenes-iphone-apps.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C Conditional Operator aka Ternary Operator</title>
		<link>http://iPhoneDeveloperTips.com/c/c-conditional-operator-aka-ternary-operator.html</link>
		<comments>http://iPhoneDeveloperTips.com/c/c-conditional-operator-aka-ternary-operator.html#comments</comments>
		<pubDate>Tue, 12 Jan 2010 02:57:50 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5244</guid>
		<description><![CDATA[I&#8217;ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator.
The conditional looks as follow:
condition-test ? first-expression : second-expression

If the condition-test is nonzero, the first-expression is evaluated, otherwise [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator.</p>
<p>The conditional looks as follow:</p>
<p><strong>condition-test ? first-expression : second-expression<br />
</strong><br />
If the condition-test is nonzero, the first-expression is evaluated, otherwise the second-expression is evaluated.<br />
<span id="more-5244"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Traditional approach</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span>;
<span style="color: #a61390;">else</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Conditional operator</span>
str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;</pre></div></div>

<h5>Nesting Conditional Operators</h5>
<p>Extending the example above, we can update the code for the case where x is neither positive or negative (x is zero), using nested conditionals:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Traditional approach</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span>;
<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span>;
<span style="color: #a61390;">else</span>
  str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Conditional operator</span>
str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> x &lt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span>;</pre></div></div>

<p>As shown above, based on the association of parameters, no parenthesis are necessary, however, I find this much easier to read:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">str <span style="color: #002200;">=</span> x &gt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;positive&quot;</span> <span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span>x &lt; <span style="color: #2400d9;">0</span> ? <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;negative&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;zero&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Good Use Case</h5>
<p>I find the conditional operator useful when creating messages in which part of the message may be plural or singular depending on the value of a variable. </p>
<p>It&#8217;s not much more work to make messages appear as expected, for example, how often have you seen a message from an application that looks similar to this &#8220;<strong><em>1 file(s) downloaded</em></strong>&#8221; where ideally the message would read &#8220;<strong><em>1 file downloaded</em></strong>&#8221; &#8211; not a big deal, however, it&#8217;s a trivial amount of code to get this right:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <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;%d file%s downloaded&quot;</span>, x, <span style="color: #002200;">&#40;</span>x &gt; <span style="color: #2400d9;">1</span> ? <span style="color: #bf1d1a;">&quot;s&quot;</span> <span style="color: #002200;">:</span> <span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Sometimes it&#8217;s the little things that will setup you apart in a crowd of developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/c/c-conditional-operator-aka-ternary-operator.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API&#8217;s</title>
		<link>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:26:13 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5020</guid>
		<description><![CDATA[It&#8217;s been covered by a number of websites and blogs on how to play movies in portrait mode using MPMoviePlayerController. Problem is, every solution that I&#8217;ve been able to find uses private API&#8217;s to tell the player to flip the direction of play.
Other than the built-in movie player, another option is to use a UIWebview, [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been covered by a number of websites and blogs on how to play movies in portrait mode using <strong>MPMoviePlayerController</strong>. Problem is, every solution that I&#8217;ve been able to find uses private API&#8217;s to tell the player to flip the direction of play.</p>
<p>Other than the built-in movie player, another option is to use a <strong>UIWebview</strong>, however, there are a few drawbacks including no support for notifications on when a movie has been preloaded, which is handy for displaying a &#8220;please wait&#8221; loading message.</p>
<p>This post is introduces another approach to playing a movie in portrait mode without delving into private API&#8217;s, and I think you&#8217;ll agree, it&#8217;s quite clever.<br />
<span id="more-5020"></span></p>
<h5>Portrait Video Mode on the iPhone</h5>
<p>I spend a fair amount of time working with video related applications and content, so when I saw a portrait video playing seamlessly inside <a  target="_blank"  href="http://itunes.apple.com/us/app/dog-tricks-best-101-dog-tricks/id311117783?mt=8">Dog Tricks &#8211; Best of 101 Dog Tricks</a> I was very interested to figure out how to do the same.</p>
<p>I wrote Michael Schneider of <a  target="_blank"  href="http://www.hivebrainsoftware.com/">Hive Brain Software</a>, developer of the above app and asked if he would share his secret. Michael was kind enough to pass along some great notes explaining how we got this to work. This post walks through a complete working example I wrote based on the information Michael shared with me.</p>
<h5>The End Result</h5>
<p>Let&#8217;s start by looking at a short video of the application I&#8217;ll build in this post running in the simulator:</p>
<p><embed src="http://iphonedevelopertips.com/wp-content/uploads/2010/01/port.mov" width="320" height="430" autoplay="false"> </p>
<p>Notice how the video is shown within the application with the tabbar controls still visible on the screen.</p>
<h5>The 30,000 Foot View</h5>
<p>The basic idea to make this work is simple, rotate the orientation of the original video, turn off the movie player controls and fire up the movie player as normal.</p>
<p>What you end up with is the movie player running landscape mode with your video content viewable in portrait mode.  The reason for hiding the controls is that they would appear sideways. As an example, notice in the screenshot below that the video is viewable in portrait mode, however, the controls are still oriented for a landscape movie player:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp51-201x300.png" alt="" title="mp5" width="201" height="300" class="alignleft size-medium wp-image-5204" style="margin: 10px;"/></p>
<p>Although a simple trick, it&#8217;s the little things that will make this approach appear seamless within your application. As an example of a nice effect, I&#8217;ll show how to overlay an image on top of the movieplayer so it appears as though the movie is playing directly inside your app.</p>
<p>There are three steps to make this all work:</p>
<p>- Change the orientation of the movie<br />
- Turn off the movie player controls<br />
- Overlay an image of your application UI on top of the movie player</p>
<p>Let&#8217;s see how to pull all the pieces together&#8230;</p>
<h5>Video Orientation</h5>
<p>The first step is to use a capable video editing tool to rotate your video to portait mode. I used Quicktime Pro (on Snow Leopard), the steps that I used follow:</p>
<p>- From the Window menu choose Show Movie Properties<br />
- Click on the row for the video track<br />
- Click the Visual Settings tab<br />
- In the Flip/Rotate section, rotate the video counter-clockwise<br />
   (or clockwise depending on the orientation you set for your iPhone app).</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/mp2.png" alt="" title="mp2" width="300" height="228" class="alignnone size-full wp-image-5111" style="margin: 10px;"/></p>
<h5>Code for App Delegate</h5>
<p>The app delegate code for this code example is nothing more than a Window object and a Tabbar controller. The interface and implementation files are below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@class</span> TabbarController;
&nbsp;
<span style="color: #a61390;">@interface</span> Test_appAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> &lt;UIApplicationDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UIWindow <span style="color: #002200;">*</span>window;
  TabbarController <span style="color: #002200;">*</span>tabbar;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> TabbarController <span style="color: #002200;">*</span>tabbar;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The implementation file:</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><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>   
  <span style="color: #11740a; font-style: italic;">// Create and initialize the window</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;">// Create the tabbar</span>
  tabbar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TabbarController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>tabbar.view<span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
...</pre></div></div>

<h5>Code for Tabbar Controller</h5>
<p>The tabbar controller is code is nothing out of the ordinary, the interface file is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TabbarController <span style="color: #002200;">:</span> UITabBarController  &lt;UITabBarControllerDelegate&gt;
<span style="color: #002200;">&#123;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>As you can see in the code below I&#8217;ve defined three tabs, each with a unique title:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> TabbarController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init
<span style="color: #002200;">&#123;</span>
  self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    self.delegate <span style="color: #002200;">=</span> self;
&nbsp;
    UIViewController <span style="color: #002200;">*</span>localViewController;   
&nbsp;
    <span style="color: #11740a; font-style: italic;">// New tabbar controller and array to contain the view controllers</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>localViewControllersArray <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><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 1</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar1ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 1&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 2</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar2ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 2&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Tab 3</span>
    localViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Tabbar3ViewController alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tab 3&quot;</span><span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>localViewControllersArray addObject<span style="color: #002200;">:</span>localViewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localViewController release<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// Retained by above array</span>
&nbsp;
    self.viewControllers <span style="color: #002200;">=</span> localViewControllersArray;
    <span style="color: #002200;">&#91;</span>localViewControllersArray release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Code for Tab 1</h5>
<p>The only tabbar that has any working code for this example is the left-most tab, which plays the movie in portrait mode. The interface definition for this tab is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;MediaPlayer/MediaPlayer.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Tabbar1ViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  MPMoviePlayerController  <span style="color: #002200;">*</span>moviePlayer;
  <span style="color: #400080;">NSURL</span>  <span style="color: #002200;">*</span>movieURL;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Inside the implementation file, the first block of relevant code is the initialization, which sets up the view, the background color and the title for the tab:</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><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theTitle 
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView 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> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;    
    self.view.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blackColor<span style="color: #002200;">&#93;</span>;
    self.title <span style="color: #002200;">=</span> theTitle;
  <span style="color: #002200;">&#125;</span>  
  <span style="color: #a61390;">return</span> self;  
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The next section of code to write is when the tab is about to appear, here we create the path to the movie, setup the movie player, register to receive a notification when the movie has finished playing, start the movie, and finally, overlay an image on top of the player (more on that below):</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
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Path to the movie</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mov&quot;</span><span style="color: #002200;">&#93;</span>;      
  movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup the player</span>
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Hide the controls</span>
  <span style="color: #002200;">&#91;</span>moviePlayer setMovieControlMode<span style="color: #002200;">:</span>MPMovieControlModeHidden<span style="color: #002200;">&#93;</span>;  
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie has finished playing</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
                      selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
                      name<span style="color: #002200;">:</span>MPMoviePlayerPlaybackDidFinishNotification 
                      object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start the movie  </span>
  <span style="color: #002200;">&#91;</span>moviePlayer play<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Overlay an image with center that is transparent for movie to show through  </span>
  MovieOverlayViewController <span style="color: #002200;">*</span>overlay <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MovieOverlayViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>windows <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> windows<span style="color: #002200;">&#93;</span>;
  UIWindow <span style="color: #002200;">*</span>moviePlayerWindow <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>windows objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>moviePlayerWindow addSubview<span style="color: #002200;">:</span>overlay.view<span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The interesting code starts on line 21, this is where I define a new view controller, <strong>MovieOverlayViewController</strong>, which will overlay an image on top of the running movie player. This overlay will present the illusion that the movie is running directly inside the app.</p>
<h5>Movie Image Overlay</h5>
<p>To give the app the appearance of running natively in portrait mode, we&#8217;ll overlay an image that shows the tabbars along the bottom and a text banner across the top. I&#8217;ll also write code to detect when the user taps on the image and do the necessary checks to see if the user tapped in an area that would translate to one of the tabs, more on that momentarily.</p>
<p>The finished look on the device is below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp6-200x300.png" alt="" title="mp6" width="200" height="300" class="alignnone size-medium wp-image-5207" /></p>
<p>Notice in the figure below that actual image we will overlay is nothing more than a transparent image with some text across the top and a series of tabbar images across the bottom.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/mp7-200x300.png" alt="" title="mp7" width="200" height="300" class="alignnone size-medium wp-image-5208" /></p>
<p>The movie overlay view controller for this app is quite trivial, it contains nothing more than a <strong>UIImageView</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MovieOverlayViewController <span style="color: #002200;">:</span> UIViewController 
<span style="color: #002200;">&#123;</span>
  UIImageView <span style="color: #002200;">*</span>overlay;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Inside the implementation file, we begin with the initialization and code for creating the <strong>UIImageView</strong> that will be overlayed on the movie player:</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><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>init
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    self.view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView 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> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;    
  <span style="color: #a61390;">return</span> self;  
<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> viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated
<span style="color: #002200;">&#123;</span>
  overlay <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</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><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;overlay.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>overlay<span style="color: #002200;">&#93;</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The other relevant code for this view controller is managing touches on the image. To provide the most realistic user experience, once the image is overlayed and the movie is playing, you will need to detect touches on the image where the tabs live. The code below will determine if a touched point is within the rectangle of each of the tabs, printing a message to the console on which tab was touched.</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><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event 
<span style="color: #002200;">&#123;</span>	  
  <span style="color: #11740a; font-style: italic;">// Detect touch anywhere</span>
  UITouch <span style="color: #002200;">*</span>touch <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touches anyObject<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Where is the point touched</span>
  CGPoint point <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>touch locationInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>; 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pointx: %f pointy:%f&quot;</span>, point.x, point.y<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Was a tab touched, if so, which one...</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 1 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">107</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 2 touched&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>CGRectContainsPoint<span style="color: #002200;">&#40;</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">214</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">106</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>, point<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tab 3 touched&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>You could add code to the above example and when tab 2 or 3 is tapped, stop the movie, remove the overlay and switch to the view controller associated with the appropriate tab.</p>
<h5>Xcode Portrait Mode Video Source Code</h5>
<p>You can download the entire Xcode project here: <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/01/XcodePortraitVideo.zip">Xcode Portrait Video</a>.</p>
<h5>Credits</h5>
<p>Thanks again to <a  target="_blank"  href="http://www.hivebrainsoftware.com/">Michael Schneider of Hive Brain Software</a> for sharing his insight on this trick. You can check out more of Michael&#8217;s work on a series of self improvement applications at <a  target="_blank"  href="http://www.relaxingapps.com/">Relaxing Apps</a>.</p>
<p>And Michael would like to pass on many thanks Scott Michaels at Atimi in Vancouver BC for sharing information on this approach at the 360iDev conference in San Jose.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://iphonedevelopertips.com/wp-content/uploads/2010/01/port.mov" length="306215" type="video/quicktime" />
		</item>
		<item>
		<title>Own an iPhone or iPod touch? I Could Use Your Help To Test a Streaming Video App</title>
		<link>http://iPhoneDeveloperTips.com/iphone-apps/own-an-ipod-touch-i-could-use-your-help-to-test-a-streaming-video-app.html</link>
		<comments>http://iPhoneDeveloperTips.com/iphone-apps/own-an-ipod-touch-i-could-use-your-help-to-test-a-streaming-video-app.html#comments</comments>
		<pubDate>Tue, 29 Dec 2009 18:21:33 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[iPhone Apps]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5180</guid>
		<description><![CDATA[Cartoons to Go is a streaming video application that plays vintage cartoons (from 1920&#8217;s through 1950&#8217;s). Recently there have been a handful of comments about the application not working as expected.
I am looking for 10-15 people who own an iPhone or iPod touch to give the application a try. I&#8217;ll create a build specifically for [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://itunes.apple.com/us/app/cartoons-to-go/id305713868?mt=8">Cartoons to Go</a> is a streaming video application that plays vintage cartoons (from 1920&#8217;s through 1950&#8217;s). Recently there have been a handful of comments about the application not working as expected.</p>
<p>I am looking for 10-15 people who own an iPhone or iPod touch to give the application a try. I&#8217;ll create a build specifically for your device or provide a promo code to download the latest version.</p>
<p>What I ask for in return is a thorough run through of the app, testing the streaming speed and performance, as well as exercising the overall UI.</p>
<p>Please send me an email if you are interested to help out jwmuchow at gmail dot com</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/iphone-apps/own-an-ipod-touch-i-could-use-your-help-to-test-a-streaming-video-app.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing iTunes Reports When iTunes is Closed</title>
		<link>http://iPhoneDeveloperTips.com/itunes/accessing-itunes-reports-when-itunes-is-closed.html</link>
		<comments>http://iPhoneDeveloperTips.com/itunes/accessing-itunes-reports-when-itunes-is-closed.html#comments</comments>
		<pubDate>Mon, 28 Dec 2009 19:00:20 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[iTunes]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5162</guid>
		<description><![CDATA[Over the holiday Apple shutdown iTunes:

Even though you can&#8217;t use iTunes to upload new apps, change current app descriptions, prices, etc, you can access your reports with the following link: https://itts.apple.com
]]></description>
			<content:encoded><![CDATA[<p>Over the holiday Apple shutdown iTunes:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/closed.png" alt="" title="closed" width="500" height="209" class="alignnone size-full wp-image-5170" /></p>
<p>Even though you can&#8217;t use iTunes to upload new apps, change current app descriptions, prices, etc, you can access your reports with the following link: <a href="https://itts.apple.com">https://itts.apple.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/itunes/accessing-itunes-reports-when-itunes-is-closed.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NSRange and NSString Objects</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html#comments</comments>
		<pubDate>Thu, 24 Dec 2009 17:52:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5132</guid>
		<description><![CDATA[When poking around NSString methods you&#8217;ll find many references to NSRange, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and [...]]]></description>
			<content:encoded><![CDATA[<p>When poking around <strong>NSString</strong> methods you&#8217;ll find many references to <strong>NSRange</strong>, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and number of elements needed (examples to follow). </p>
<h5>NSRange Definition</h5>
<p>NSRange is a structure defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> _NSRange 
<span style="color: #002200;">&#123;</span>
  NSUInteger location;
  NSUInteger length;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">NSRange</span>;</pre></div></div>

<p><span id="more-5132"></span></p>
<p><strong>location</strong> is the starting index in the range (zero based) and <strong>length</strong> is the number of entries in the range. <strong>NSUInteger</strong> is simply an unsigned value that supports both 32 and 64 bit systems. Here is how <strong>NSUInteger</strong> is defined:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">long</span> NSUInteger;
<span style="color: #6e371a;">#else</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> NSUInteger;
<span style="color: #6e371a;">#endif</span></pre></div></div>

<h5>NSRange and Strings</h5>
<p>The example below shows one approach for creating a range and using the same to extract a substring &#8211; the output from below is <strong><em>IPA</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Starting at position 25, get 3 characters</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> NSMakeRange <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">25</span>, <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// This would also work:</span>
<span style="color: #11740a; font-style: italic;">// NSRange range = {25, 3};</span>
&nbsp;
NSLog <span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer shortname: %@&quot;</span>, <span style="color: #002200;">&#91;</span>homebrew substringWithRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>If you want to search for a substring, you could write something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Did we find the string &quot;IPA&quot; ?</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from above will display: <strong><em>Range is: {25, 3}</em></strong>. Notice the call to <strong>NSStringFromRange()</strong> which will display the return value (a range) as an <strong>NSString</strong>. There is also a function to create a range from a string: <strong>NSRangeFromString()</strong>.</p>
<p>Let&#8217;s look at one more example, the code below will search for the string &#8220;ia&#8221; starting at the end of the string moving towards the beginning:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>homebrew <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Imperial India Pale Ale (IPA)&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Search for the &quot;ia&quot; starting at the end of string</span>
<span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>homebrew rangeOfString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ia&quot;</span> options<span style="color: #002200;">:</span>NSBackwardsSearch<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// What did we find</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>range.length &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Range is: %@&quot;</span>, NSStringFromRange<span style="color: #002200;">&#40;</span>range<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The result from above is: <strong><em>Range is: {12, 2}</em></strong> (the &#8220;ia&#8221; inside the word &#8220;India&#8221;).</p>
<h5>NSRange Functions</h5>
<p>Here is a list of functions that work with ranges:</p>
<p>NSEqualRanges()<br />
NSIntersectionRange()<br />
NSLocationInRange()<br />
NSMakeRange()<br />
NSMaxRange()<br />
NSRangeFromString()<br />
NSStringFromRange()<br />
NSUnionRange()</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/nsrange-and-nsstring-objects.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
