<?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>farp.blog &#187; uploader</title>
	<atom:link href="http://fredandrandall.com/blog/tag/uploader/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredandrandall.com/blog</link>
	<description>Bloggin&#039; about whatever</description>
	<lastBuildDate>Tue, 04 Nov 2014 07:15:30 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Simple imgur iPhone Uploader</title>
		<link>http://fredandrandall.com/blog/2011/02/26/simple-imgur-iphone-uploader/</link>
		<comments>http://fredandrandall.com/blog/2011/02/26/simple-imgur-iphone-uploader/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 23:01:27 +0000</pubDate>
		<dc:creator><![CDATA[Randall]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[imgur]]></category>
		<category><![CDATA[iPhone SDK]]></category>
		<category><![CDATA[objc]]></category>
		<category><![CDATA[uploader]]></category>

		<guid isPermaLink="false">http://fredandrandall.com/blog/?p=259</guid>
		<description><![CDATA[Recently I&#8217;ve been toying with the idea of adding image uploading to Thoughtback. One easy way to do that is to use another image host and just post a link in the thought. I decided to try out imgur because &#8230; <a href="http://fredandrandall.com/blog/2011/02/26/simple-imgur-iphone-uploader/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><a href="http://fredandrandall.com/blog/wp-content/uploads/2011/02/imgurimage.png"><img class="alignleft size-full wp-image-275" title="imgurimage" src="http://fredandrandall.com/blog/wp-content/uploads/2011/02/imgurimage.png" alt="" width="333" height="134" /></a>Recently I&#8217;ve been toying with the idea of adding image uploading to <a href="http://www.thoughtback.com">Thoughtback</a>. One easy way to do that is to use another image host and just post a link in the thought. I decided to try out <a href="http://www.imgur.com">imgur</a> because of their <a href="http://api.imgur.com/resources_anon">Anonymous API</a>. What it lets you do is upload images to their service, without requiring a username and password. This is great if you just want quick and easy image sharing.</p>
<p>To get started imgur has some good <a href="http://api.imgur.com/examples">examples</a> of how to upload in a variety of programming languages. I grabbed the iPhone example and copy and pasted it in thinking, &#8220;Well that was easy&#8221;. It turns out there was a little bit more to do.</p>
<p><span id="more-259"></span>Here is the iOS example from their website (with a few typos corrected)</p>
<pre class="brush: objc; title: ; notranslate">
-(NSData *)uploadPhoto:(UIImage *) image {
       NSData   *imageData  = UIImageJPEGRepresentation(image,1);
       NSString *imageB64   = [self escapeString:[imageData base64Encoding]];  //Custom implementations, no built in base64 or HTTP escaping for iPhone
       NSString *uploadCall = [NSStringstringWithFormat:@&quot;key=%@ℑ=%@&quot;,imgurKey,imageB64];

       NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@&quot;http://api.imgur.com/2/upload&quot;]];
       [request setHTTPMethod:@&quot;POST&quot;];
       [request setValue:[NSString stringWithFormat:@&quot;%d&quot;,[uploadCalllength]] forHTTPHeaderField:@&quot;Content-length&quot;];
       [request setHTTPBody:[uploadCall dataUsingEncoding:NSUTF8StringEncoding]];

       NSURLResponse *response;
       NSError *error = nil;

       NSData *XMLResponse= [NSURLConnection sendSynchronousRequest:requestreturningResponse:&amp;response error:&amp;error];

       return XMLResponse;
}
</pre>
<p>The problem with their example was that it wasn&#8217;t quite finished.<br />
<a href="http://fredandrandall.com/blog/wp-content/uploads/2011/02/apiProblem1.png"><img class="aligncenter size-full wp-image-267" title="apiProblem" src="http://fredandrandall.com/blog/wp-content/uploads/2011/02/apiProblem1.png" alt="" width="614" height="60" /></a>That <code>Database64Encoding</code> method is not a part of the iOS sdk. You need to do that yourself. So what does it mean exactly?</p>
<p>It means that you need to convert the image data to <a href="http://en.wikipedia.org/wiki/Base64">Base64</a>. That basically means turning your image data into a string so it can be sent up via a POST request. So I did a little bit of Googling and found some code that converts NSData to a Base64 NSString. I got the code from the <a href="http://mattgemmell.com/2008/02/22/mgtwitterengine-twitter-from-cocoa">MGTwitterEngine</a>, but it can be found in a few places.</p>
<p>So after that I thought I should be done. I ran the code and got an error back from imgur. Then I noticed the weird ℑ character in the request. I&#8217;m not sure what it&#8217;s for, but it didn&#8217;t work. I replaced it with &#8220;image&#8221; and I got a success back from imgur.</p>
<p>Hooray, everything seemed like it was working, but it still wasn&#8217;t. When I went to the URL in the response, there was no image. Nothing loaded. What was wrong?</p>
<p>I had a suspicion that there was some funny stuff going on with the encoding so I wrote the base64 encoded string out to a file (it was waaaay too long for NSLog to handle) to see what was actually in it. There were a bunch of /&#8217;s and +&#8217;s. These are valid URL characters that can definitely mess up query string parsing if you want them to be in one of your arguments.</p>
<p>I pulled out an NSString category I had written to escape valid URL characters and called my <code>stringByEscapingValidURLCharacters</code> method on the base64 string. After trying again I was getting valid stuff back and my image URLs were loading correctly.</p>
<p>So, to prevent others from going through the headaches to make a simple imgur uploader, I decided to open source some example code code.</p>
<p>This code is very simplistic. It is a simple iPhone app that lets you choose an image from the camera or your phone&#8217;s library and send it to imgur. The ImgurUploader class calls out to imgur and returns the URL of the original sized image by calling a delegate method. It doesn&#8217;t handle any errors right now either. You will also need to get an <a href="http://imgur.com/register/api/">API key</a> from imgur, which only takes a second. This comes with the &#8220;works on my machine&#8221; <a href="http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html">seal</a> of approval.</p>
<p><a href="https://github.com/blladnar/iPhone-Imgur-Uploader">Check it out on GitHub</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fredandrandall.com/blog/2011/02/26/simple-imgur-iphone-uploader/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
