<?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>WebSight Design Blog &#187; cocoa touch</title>
	<atom:link href="http://blog.websightdesign.com/tag/cocoa-touch/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.websightdesign.com</link>
	<description>The Official Blog of WebSight Design, Inc.</description>
	<lastBuildDate>Tue, 27 Jul 2010 23:03:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using the UIImagePickerController in an iPhone OS v2.2.1 app &amp; On A OS v3.0 Device</title>
		<link>http://blog.websightdesign.com/2009/08/31/using-the-uiimagepickercontroller-in-an-iphone-os-v2-2-1-app-on-a-os-v3-0-device/</link>
		<comments>http://blog.websightdesign.com/2009/08/31/using-the-uiimagepickercontroller-in-an-iphone-os-v2-2-1-app-on-a-os-v3-0-device/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 18:50:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://blog.websightdesign.com/?p=225</guid>
		<description><![CDATA[So I wrote an iPhone application for a client using v2.2.1 of the iPhone SDK. (The client did not want to go OS v3.0 yet.) I needed to give a user the capability of taking and uploading a picture with the built-in camera or uploading a picture from the photo library on the phone. Seems [...]<p><a href="http://blog.websightdesign.com/2009/08/31/using-the-uiimagepickercontroller-in-an-iphone-os-v2-2-1-app-on-a-os-v3-0-device/">Using the UIImagePickerController in an iPhone OS v2.2.1 app &#038; On A OS v3.0 Device</a> is a post from: WebSight Design, Inc. company blog. With headquarters in the San Francisco bay area, WebSight Design, Inc. has been providing world class web engineering, creative design and hosting solutions since 1995. <a href="http://Websightdesign.com">WebSight Design</a></p>
]]></description>
			<content:encoded><![CDATA[<p>So I wrote an <a href="http://www.websightdesign.com/services/iphone">iPhone application</a> for a client using v2.2.1 of the iPhone SDK. (The client did not want to go OS v3.0 yet.) I needed to give a user the capability of taking and uploading a picture with the built-in camera or uploading a picture from the photo library on the phone. Seems pretty straightforward so far. Yes? I thought so. It turned out to be pretty tricky but the solution was very simple and was staring me in the face.</p>
<p><strong>The Problem</strong><br />
I had a UIViewController with an UIImageView setup to be used as a container for the selected image. I also had several UIButtons on the page. (One button for calling up the camera. Another button for calling up the photo library, etc&#8230;) So what should have happened is that the Camera or photo library UI would display a Modal View window the user instructing them to either take a picture or select a photo from their library. Once they did that, they would be returned to the main view and whatever photo they took or selected would appear in UIImageView as a preview. However, when I was selecting the photo the &#8220;editingInfo&#8221; NSDictionary object that was supposed to be returned would be there only about 50% of the time. The other 50% it would be returned empty. I double and triple checked my code and it was all correct. Something was zapping my dictionary object and I couldn&#8217;t, for the life of me, figure out what it was.</p>
<p>The second part to this problem was that the preview image never appeared whenever the user was grabbing their image from the camera. It worked if the photo library was the source but not if it was the camera. Very, very frustrating.</p>
<p>Here is my original code:</p>
<p>My .h file -</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@interface</span> AddPhotoController <span style="color: #002200;">:</span> UIViewController &nbsp;<span style="color: #002200;">&#123;</span><br />
IBOutlet UIImageView <span style="color: #002200;">*</span>imageView;<br />
IBOutlet UIButton <span style="color: #002200;">*</span>snapNewPictureButton;<br />
IBOutlet UIButton <span style="color: #002200;">*</span>selectFromPhotoLibraryButton;<br />
<span style="color: #002200;">&#125;</span><br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIImageView <span style="color: #002200;">*</span>imageView;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIButton <span style="color: #002200;">*</span>snapNewPictureButton;<br />
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIButton <span style="color: #002200;">*</span> selectFromPhotoLibraryButton;</div></div>
<p>My .m file -</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #a61390;">@implementation</span> AddPhotoController<br />
<span style="color: #a61390;">@synthesize</span> imageView, snapNewPictureButton, selectFromPhotoLibraryButton;</div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>getCameraPicture<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender<br />
<span style="color: #002200;">&#123;</span><br />
UIImagePickerController <span style="color: #002200;">*</span>picker <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImagePickerController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;<br />
picker.delegate <span style="color: #002200;">=</span> self;<br />
picker.sourceType <span style="color: #002200;">=</span> UIImagePickerControllerSourceTypeCamera;<br />
picker.allowsImageEditing <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>picker animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#91;</span>picker release<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>imagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image editingInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>editingInfo<br />
<span style="color: #002200;">&#123;</span><br />
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Image Meta Info.: %@&quot;</span>,editingInfo<span style="color: #002200;">&#41;</span>;</div></div>
<p>UIImage *selectedImage = image;<br />
imageView.image = selectedImage;<br />
self._havePictureData = YES;<br />
[self.useThisPhotoButton setEnabled:YES];</p>
<p>[picker dismissModalViewControllerAnimated:YES];<br />
}</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>imagePickerControllerDidCancel<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #002200;">&#91;</span>picker dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>Like I said before, it all seems pretty straightforward. However, there were two things wrong with the above code.<br />
#1. Deprecated Method<br />
The UIImagePickerController method, <strong>– imagePickerController:didFinishPickingImage:editingInfo:</strong>, is deprecated in v3.0 of the iPhone OS. So even though I built the app using SDK v2.2.1, because the app will be running on 3.0 devices, I needed to use the new and improved method, <strong>- imagePickerController:didFinishPickingMediaWithInfo:editingInfo</strong>, which contains FIVE pieces of really important information:</p>
<ol>
<li> UIImagePickerControllerMediaType &#8211; Which specifies the type of media that this dictionary contains.</li>
<li> UIImagePickerControllerOriginalImage &#8211; Contains the original non-cropped image.</li>
<li> UIImagePickerControllerEditedImage &#8211; Contains the user-edited cropped image.</li>
<li> UIImagePickerControllerCropRect &#8211; Contains the CGRect that was applied to the original image.</li>
<li> UIImagePickerControllerMediaURL &#8211; If the object is a movie, this object contains the URL to be used by the media player.</li>
</ol>
<p>I simply accessed the &#8220;UIImagePickerControllerEditedImage&#8221; object from the dictionary and was off to the races.</p>
<p>#2. Dismissing the Modal View<br />
This one was really painful as it was so simple to solve. In order for the UIImageView to be updated correctly, you must dismiss the ImagePicker modal window BEFORE you attempt to update your UIImageView.</p>
<p>As a result of this, here is my updated method call:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> imagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>thePicker didFinishPickingMediaWithInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageInfo<br />
<span style="color: #002200;">&#123;</span><br />
<span style="color: #002200;">&#91;</span>thePicker dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
UIImage <span style="color: #002200;">*</span>img <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>imageInfo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UIImagePickerControllerEditedImage&quot;</span><span style="color: #002200;">&#93;</span>;<br />
previewImage.image <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;<br />
self.previewImage.image <span style="color: #002200;">=</span> img;</div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>img<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>imageData length<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span></div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self archivePictureData<span style="color: #002200;">:</span>imageData<span style="color: #002200;">&#93;</span>;<br />
self._havePictureData <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span>self.useThisPhotoButton setEnabled<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#125;</span></div></div>
<p>That was it. Hope that was helpful.</p>
<p><a href="http://blog.websightdesign.com/2009/08/31/using-the-uiimagepickercontroller-in-an-iphone-os-v2-2-1-app-on-a-os-v3-0-device/">Using the UIImagePickerController in an iPhone OS v2.2.1 app &#038; On A OS v3.0 Device</a> is a post from: WebSight Design, Inc. company blog. With headquarters in the San Francisco bay area, WebSight Design, Inc. has been providing world class web engineering, creative design and hosting solutions since 1995. <a href="http://Websightdesign.com">WebSight Design</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.websightdesign.com/2009/08/31/using-the-uiimagepickercontroller-in-an-iphone-os-v2-2-1-app-on-a-os-v3-0-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;ve Fallen In Love&#8230; Her Name is Cocoa!</title>
		<link>http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/</link>
		<comments>http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/#comments</comments>
		<pubDate>Sun, 24 Aug 2008 03:54:30 +0000</pubDate>
		<dc:creator>Lawrence</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[cocoa touch]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/</guid>
		<description><![CDATA[I’ve recently *really* been getting into Cocoa development for both the MacOSX and the iPhone. Can I just say that Cocoa, (especially Cocoa Touch), ROCKS!!! I am in the middle of building a soon-to-be-published app for the Apple App Store. I forgot how much I loved writing C code. Objective-C is really a nice, simple [...]<p><a href="http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/">I&#8217;ve Fallen In Love&#8230; Her Name is Cocoa!</a> is a post from: WebSight Design, Inc. company blog. With headquarters in the San Francisco bay area, WebSight Design, Inc. has been providing world class web engineering, creative design and hosting solutions since 1995. <a href="http://Websightdesign.com">WebSight Design</a></p>
]]></description>
			<content:encoded><![CDATA[<p style="clear: both">I’ve recently *really* been getting into Cocoa development for both the MacOSX and the iPhone. Can I just say that Cocoa, (especially Cocoa Touch), ROCKS!!! I am in the middle of building a soon-to-be-published app for the Apple App Store. I forgot how much I loved writing C code. Objective-C is really a nice, simple and clean language. I’ll be posting some tutorials on it soon.</p>
<p><br class="final-break" style="clear: both" /></p>
<p><a href="http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/">I&#8217;ve Fallen In Love&#8230; Her Name is Cocoa!</a> is a post from: WebSight Design, Inc. company blog. With headquarters in the San Francisco bay area, WebSight Design, Inc. has been providing world class web engineering, creative design and hosting solutions since 1995. <a href="http://Websightdesign.com">WebSight Design</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.websightdesign.com/2008/08/23/ive-fallen-in-love-her-name-is-cocoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
