18
Mar
2013
 

Anonymous Image File Upload in iOS With Imgur

by Matt Long

It seems like a pretty useful feature, anonymous image file upload in iOS with imgur. If you need to upload images and don’t want to fool with some authentication mess like OAuth this technique is perfect. There are libraries that have simplified the OAuth process, however, it’s nice to be able to just initiate an upload, get a result URL back and be on your way. No fuss, no muss. That’s what I was looking for, so I wrote a little app that demonstrates how to do exactly that.

Imgur and You

There are numerous image upload services out there, but imgur seems to be the simplest to implement. Now, that being said, it’s pretty difficult to find any sample code out there that is up to date and works with their v3 API. I found a library that is several years old and only works with the v2 API, but that seemed odd to me considering how long their v3 API has been around. This means either developers don’t have a need for this kind of service or that getting it to work is too obscure and/or difficult. The bottom line for me was realizing that the API is expecting a multi-part form with properly formatted data parameters. Once I figured that out, it was pretty straight forward. I wrote a little demo app you can get on github that shows how to do it.

The first thing you need to do, though, is head over to https://api.imgur.com/oauth2/addclient and register your app. A client ID will be generated and emailed to you. You can then plug it in and start posting images.

The Basic Implementation

All you need to do is call a class method on my MLIMGURUploader class and initiate your upload with an NSData object and your client ID.

This demo app just takes an NSData containing the image that we just added to the project (retrieved with a call to [UIImage imageNamed:]), a title, and a description and passes them on to the uploader.

Anonymous Image File Upload in iOS With Imgur:IMGUR Uploader

When the request returns, you receive back an NSString containing the URL to the newly created image.

Code Purity

It’s often difficult to achieve code purity where you’re only using built in libraries to develop your apps, but over the years I’ve become much more of a purist and try to eliminate the need for libraries as much as possible. I haven’t always been that way. Shortcuts seem like a good idea when you’re in a hurry to get something done, but in the end they often just end up biting you.

With this code purity idea in mind, here is the result for the imgur uploader–all in a single class method that only uses built in foundation libraries:

Request Results

A successful request will provide a URL that points to the raw image you uploaded to the service. If you would prefer to access the imgur link that displays their site chrome and the title and description your provided in the upload, simply remove the file extension. So, for example, the url http://i.imgur.com/bQzUcIp.jpg will provide the raw image, the link http://i.imgur.com/bQzUcIp provides the imgur site wrapped version.

Conclusion

I’m glad that in the end this process of anonymously uploading an image file was pretty trivial. Though you never know with some of these services these days–they could change the way it works at any time. Until then, though, I hope it will help you as well. Until next time.

Get the project on GitHub