<?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>Chris-Software.com &#187; using UIImagePickerController</title>
	<atom:link href="http://chris-software.com/index.php/tag/using-uiimagepickercontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-software.com</link>
	<description>iPhone, iPod touch games, Objective-C Tutorials, krzysztofrutkowski.com</description>
	<lastBuildDate>Tue, 29 Mar 2011 09:58:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Loading images</title>
		<link>http://chris-software.com/index.php/2009/04/29/loading-images/</link>
		<comments>http://chris-software.com/index.php/2009/04/29/loading-images/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 11:45:07 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[from albums]]></category>
		<category><![CDATA[load photo]]></category>
		<category><![CDATA[Loading pictures]]></category>
		<category><![CDATA[photo]]></category>
		<category><![CDATA[pictures]]></category>
		<category><![CDATA[using UIImagePickerController]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=723</guid>
		<description><![CDATA[The Photos on every single iPhone / iPod touch can be shared with others apps &#8211; even your own. Using UIImagePickerController, you are allowed to access any photo from the device or even take a new one with the camera.
You might have never noticed it, but the same method I&#8217;m going to present you, is used [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-724" title="uiimagepicker" src="http://chris-software.com/wp-content/uploads/2009/04/uiimagepicker-161x300.png" alt="uiimagepicker" width="161" height="300" />The Photos on every single iPhone / iPod touch can be shared with others apps &#8211; even your own. Using <em>UIImagePickerController</em>, you are allowed to access any photo from the device or even take a new one with the camera.</p>
<p>You might have never noticed it, but the same method I&#8217;m going to present you, is used in the official Apple built-in applications like<em> Contacts</em>, when you assign a photo to any contact, and <em>Settings</em> while choosing the wallpaper.</p>
<p>This method is not only useful in creating next Photo manager apps  or simple editors.</p>
<p>Before you start you need to add two protocols to your class (header <strong>.h</strong>) which will handle <em>UIImagePickerController</em>. These two protocols  are: <em>UIImagePickerControllerDelegate</em> and <em>UINavigationControllerDelegate</em>. Your <em>interface</em> code should look like this:</p>
<pre><span>@interface</span> MyView : UIViewController &lt;UIImagePickerControllerDelegate, UINavigationControllerDelegate&gt; {</pre>
<p>Once you&#8217;ve done that it&#8217;s time to implement a method that will show the <em>UIImagePickerController</em>. I use:</p>
<pre> 

-(<span>IBAction</span>)openAlbums {
<span><span>	</span></span><span>if</span><span> ([</span><span>UIImagePickerController</span><span> </span>isSourceTypeAvailable<span>:</span><strong>UIImagePickerControllerSourceTypePhotoLibrary</strong><span>]) {
<span><span>		</span></span>UIImagePickerController<span> *</span><span>UIPicker</span><span> = [[</span>UIImagePickerController<span> </span><span>alloc</span><span>] </span><span>init</span><span>];
<span><span>		</span></span><span>UIPicker</span><span>.</span>allowsImageEditing<span> = </span><span><strong>NO</strong></span><span>;
<span>		</span><span>UIPicker</span>.<span>delegate</span> = <span>self</span>;<span>
</span><span><span>		</span></span><span>UIPicker</span><span>.</span><span>sourceType</span><span> = </span><strong>UIImagePickerControllerSourceTypePhotoLibrary</strong><span>;
<span><span>		</span>[</span><span>self</span><span> </span>presentModalViewController<span>:</span><span>UIPicker</span><span> </span>animated<span>:</span><span>YES</span><span>];
<span>	</span>}
<span><span>	</span></span>else<span> {
<span><span>		</span></span><span>UIAlertView</span><span> *alert = [[</span><span>UIAlertView</span><span> </span>alloc<span>] </span>initWithTitle<span>:</span><span>nil</span><span> </span>message<span>:</span><span>@"Error"</span><span> </span>delegate<span>:</span><span>self</span><span> </span>cancelButtonTitle<span>:</span><span>@"Cancel"</span><span> </span>otherButtonTitles<span>:</span><span>nil</span><span>];
<span>		</span>[alert <span>show</span>];
<span>		</span>[alert <span>release</span>];
<span>	</span>}
}</span></span></span></span></span></span></span></pre>
<p><em>openAlbums</em> method as it&#8217;s an <em>IBAction</em> will be called by pressing a button in my case. In bold I highlighted the most important parts. The <em>if statement</em>, checks if asked <em>source type</em> is available (for example, <em>camera</em> is not available on the iPod Touch). If yes, I&#8217;m allocating the <em>UIImagePickerController</em> setting <em>delegate</em>, <em>source type</em>, and disable edition, otherwise the alert-error is displayed.</p>
<p><strong>Please note #1:</strong> always set to the same property for <em>sourceType</em> and <em>isSourceTypeAvailable</em>. Otherwise, checking if source type is available is pointless.</p>
<p><strong>Please note #2:</strong> if you set <em>YES</em> to the property <em>allowsImageEditing</em>, you have the same options of &#8220;edition&#8221; like in setting the wallpaper, that is resizing and moving.</p>
<p><em>UIImagePickerControllerSourceTypePhotoLibrary</em> shows you the list of all your albums, which you can browse to choose a photo. There are 2 others source types.</p>
<ul>
<li>UIImagePickerControllerSourceTypeSavedPhotosAlbum<br />
strictly <strong>Camera Roll</strong>, where your photos from camera, screenshots, and photos saved by applications are.</li>
<li>UIImagePickerControllerSourceTypeCamera<br />
uses the camera, to take new picture<br />
<strong>Please note:</strong> the iPhone simulator can display the camera controller but it won&#8217;t use neither iSight nor any other device as a preview. When you take the picture you get the permanent animating activity indicator and you can&#8217;t go back, only restart your app.<br />
<img class="size-medium wp-image-725 alignnone" title="simulatorcamera" src="http://chris-software.com/wp-content/uploads/2009/04/simulatorcamera-161x300.png" alt="simulatorcamera" width="97" height="180" /> <img class="alignnone size-medium wp-image-726" title="simulatorcameratook" src="http://chris-software.com/wp-content/uploads/2009/04/simulatorcameratook-161x300.png" alt="simulatorcameratook" width="97" height="180" /></li>
</ul>
<p>Before you go to the next stage, you should create an <em>UIImageView</em> (<em>IBOutlet UIImageView *imageView;</em>) and place it somewhere on your <em>view</em>, where <em>image</em> you picked, will be displayed. I recommend you to set <em>imageView&#8217;s mode</em> property to <em>Aspect Fit</em> to prevent resizing, when the picked image is bigger than <em>imageView&#8217;s</em> <em>frame</em>.</p>
<p>Now, you have to implement two more <em>UIImagePickerController</em> methods that will be called when you&#8217;ve picked an image or cancel picking.</p>
<pre>-(<span>void</span>)imagePickerController:(<span>UIImagePickerController</span> *)picker didFinishPickingImage:(<span>UIImage</span> *)image editingInfo:(<span>NSDictionary</span> *)editingInfo {
<span>	</span><span>imageView</span>.image = image;
<span><span>	</span>[[picker </span>parentViewController<span>] </span>dismissModalViewControllerAnimated<span>:</span><span>YES</span><span>];
	[picker release];
}
-(<span>void</span>)imagePickerControllerDidCancel:(<span>UIImagePickerController</span> *)picker {
<span><span>	</span>[[picker </span>parentViewController<span>] </span>dismissModalViewControllerAnimated<span>:</span><span>YES</span><span>];
	[picker release];
}</span></span></pre>
<p>The first method is called once you picked a photo using any source type, the second one is called when you cancel picking. Both methods look almost the same &#8211; both of them remove the <em>UIImagePickerController</em> by <em>dismissModalViewControllerAnimated</em><span><em>:</em></span><span><em>YES</em> and frees the memory. <em>didFinishPickingImage</em> method has one more line of code: <span><em>imageView</em></span><em>.image = image;</em>. It changes the <em>image</em> property of <em>imageView</em>, so from now <em>imageView</em> displays picked <em>image</em>.</span></p>
<h2><span>Very annoying problem:</span></h2>
<p style="text-align: center;"><span><img class="size-full wp-image-727 aligncenter" title="pickerproblem" src="http://chris-software.com/wp-content/uploads/2009/04/pickerproblem.png" alt="pickerproblem" width="361" height="199" /></span></p>
<p><span>As you see on the example above, status bar is above <em>UIImagePickerController</em> and it doesn&#8217;t look good, does it? There are two ways of solving this problem.</span></p>
<ol>
<li>Hide the status bar in either the whole your application in the <em>AppDelegate</em>, or if you need it, hide the status bar when you call <em>UIImagePickerController</em>, and show it again in in both two methods: <em>didFinishPickingImage</em> and <em>imagePickerControllerDidCancel</em>.</li>
<li>If you don&#8217;t want to use any magic tricks showing and hiding the status bar, in other words you want the status bar to be permanently visible, you are making one mistake since a longer time that has never caused any problems. Your views are of size <strong>320&#215;480px</strong> when you only need <strong>320&#215;460px</strong>. Go to your <em>view&#8217;s</em> size and set the <em>height</em> (<strong>H:</strong>) to <em>360</em>. Now in <em>App Delegate</em> where you are adding the <em>view</em> as a <em>window&#8217;s subview</em> change the <em>view&#8217;s</em> center:</li>
</ol>
<pre><span><span>	</span></span><span>MyView</span><span> *viewController = [[</span><span>MyView</span><span> </span>alloc<span>] </span>initWithNibName<span>:</span><span>@"MyView"</span><span> </span>bundle<span>:[</span><span>NSBundle</span><span> </span>mainBundle<span>]];
<span>	</span>[<span>window</span> <span>addSubview</span>:[viewController <span>view</span>]];
<span>	</span>viewController.view.center = <span>CGPointMake</span>(<span>160</span>,<span>250</span>);</span></pre>
<p><strong>Please note</strong>: on <em>iPhone Simulator</em> the <em>UIImagePickerController</em> shows immiedietly. On real device it takes few seconds (depending on the size of your <em>photo library</em>) before <em>UIImagePickerController</em> will be shown for the first time.</p>
<p>Here is my project:</p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/imagepicker.zip"><img class="alignnone size-full wp-image-564" title="xcodeproj" src="http://chris-software.com/wp-content/uploads/2009/04/xcodeproj.png" alt="xcodeproj" width="128" height="128" /></a></p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/imagepicker.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/29/loading-images/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

