iPhone dev image processing quirks

I had a lot of problems getting image processing to work properly on the iPhone. This isn’t so much because the image processing I was doing was very tricky; it was more because of some apparent quirks in how the iPhone works its image orientation.

Basically it seems that if you take a photo in portrait ie ‘up’ the iPhone thinks you’ve taken the photo in landscape ie ‘right’. Weird. But that’s how it seems to work.

In fact, no matter which way round you hold your iPhone, it always thinks the photo was taken 90 degrees clockwise from what it actually was.

In my app, I’m taking the raw image as a UIImage and converting it to a CGImage to do various things at the pixel level, and converting it back to a UIImage. As part of this I’m now therefore having to rotate the CGImage round, using code like this:

// iphone held up
if (uiimage.imageOrientation == UIImageOrientationRight) {
context1=CGBitmapContextCreate(m_imageData, m_width, m_height, 8, m_width*sizeof(uint32_t), colorSpace1, kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context1, kCGInterpolationHigh);
CGContextSetShouldAntialias(context1, YES);
CGContextTranslateCTM(context1, m_width/2, m_height/2);
CGContextRotateCTM(context1, -M_PI_2);
CGContextTranslateCTM(context1, -m_height/2, -m_width/2);
CGContextDrawImage(context1, CGRectMake(0, 0, m_height, m_width), [uiimage CGImage]);
}

This first detects which way round the iPhone thinks the photo is so we can do the right kind of rotation. It then uses CGContextTranslateCTM and CGContextRotateCTM to rotate and shift the image around in the image context space, before drawing the image into that space. Note that CGContextTranslateCTM is needed because CGContextRotateCTM rotates an image about its top left corner, not its centre. We therefore need to shift the image around if we’re ever going to see it.

The even weirder thing is that all this image rotation is unnecessary if you allow users to crop the image just after they’ve taken a photo, using the iPhone’s image picker. Obviously Apple built this kind of functionality into the image picker. The only problem with that route (which I used in my earlier Phlomo app) is that you end up with a rather small, poor quality image, and an extra unnecessary user step.

Twitmo iPhone app

twitmo iconI’ve been working on an iphone app called Twitmo which lets you take a bog-standard iPhone photo, and turns it into something a bit more interesting. You can then write a tweet by tapping on the image, and send the whole thing off to Twitter. Well, to Twitpic actually, but it’s effectively the same thing.

The philosophy behind Twitmo is the same as my earlier app, Phlomo: take quick Lomo style snaps of whatever tickles your fancy, and upload them quickly and easily to a social networking site. In this case Twitpic and Twitter.

It’s been a long, hard fight getting this app ready, mainly because of problems with image processing. Basically the iPhone has a very weird way of knowing which way up a photo should be. If you want to do real image processing this creates real headaches. I got round it in Phlomo by letting the user use the iPhone’s built-in crop, just after a photo’s taken. I don’t know how this fixed the problem, but it did. But a number of people complained that the resulting image was small and poor quality.

So, with a lot of late-night trial and error I finally got the image processing sorted.

Uploading an image to twitpic is easy. Although I’m slightly concerned that twitter are phasing in OAuth, but twitpic have made no noises about when they’re falling in line with twitter. I guess sometime later this year I may have more coding to do…

Phlomo

ok so I’ve renamed Flomo to Phlomo (there’s a toy company in the states called Flomo) and I’ve submitted the app to Apple. Fingers crossed. The whole testing and submission procedure was a bit of a pain. But now I’ve done it, it doesn’t seem too bad.

I can’t think why the app might not be accepted, although Apple are notorious for accepting the most incredible dross and rejecting perfectly good apps on minor technicalities (such as incorrect age ratings based on their bizarre criteria).

I’ve also set up Phlomo.com.

Oh and I’ve tried out this new theme, which I think is somewhat better than the last.

Flomo iphone app

icon.pngA few weeks ago I decided to start playing around with iPhone app development. It would, I thought, be a relatively straightforward thing. Even small children could do it.

But what to choose as a test project? Perhaps something stupid that made some kind of farty noise each time you shook your iPhone? Nah… build something useful Matthew. Something I and others like me would want to use.

Uploading images to Flickr seemed like a cool idea. There are some really nice apps out there that do this already. Hmmm…

Another thing was simple image manipulation. You know, make your crappy iPhone photos look like you took them with a Lomo ie turn them into something bad yet cool. Already done. Hmmm….

I know – combine the two! Brilliant idea! Let’s call it Flomo (flickr + lomo). Lame name – yes. Arsed to think up a better name – no. I even designed a funky logo (above).

Turning the idea into reality has proven to be somewhat more tricky than I’d thought. Trying to run before learning how to walk is a cliché that springs to mind. But it’s been a useful lesson in iPhone development.

Specifically it’s taught me:

1. How to access flickr’s API. Authentication with their API is like sticking hot pins in your arm at the best of times, never mind in an unfamiliar language (Objective-C).

2. Objective-C doesn’t have a simple way of doing MD5 hashes. It’s a pretty much build your own kind of thing. Ugh.

3. How to process images. Thanks to some online help and general banging-head-on-wall type thinking, I’ve finally realized that image processing isn’t all that hard. Neither is it easy. Finding stuff on forums like (I paraphrase) “listen you newbie scum, this kind of thing is so trivially easy I’m not even going to explain how to do it” didn’t help. But I got there. I’ll publish relevant bits of my code in the next post.

4. Multithreading is really useful. Do you want a nice spinny wheel thing while the image is uploading, so your user doesn’t think the app has broken? Well you *have* to use multithreading. Obvious if you know that it’s obvious. Like so much in iPhone world.

5. XML parsing? Not easy and not fun. Just parsing a simple web service response is bad enough. Making any sense at all of complex XML seems like far too much hard work. PHP SimpleXML – I love you!

There are all kinds of other bits and pieces which I’ve had “fun” with. I’ve probably implemented them really badly. But I don’t care. I have an iPhone app which works. Just a little more work and it’ll be ready to submit to Apple and wait months before it ever appears on the store for free.

Oh, and those news reports about small children coding for the iPhone? Two words: parental guidance.

IWMW2009

The Institutional Web Management Workshop (IWMW 2009) will be held at the University of Essex from Tuesday 28th to Thursday 30th July 2009.

This is generally a great chance to catch up with what’s going on in the Higher Education web world. Oh and there’s great food and lots of booze too, not that I’m swayed by such things of course…

Beginning iPhone Development: Exploring the iPhone SDK

iPhone development isn’t really all that much fun, but the end results can be very cool. Dave Mark and Jeff LaMarche have written a really nice introduction which makes the whole process seem just about manageable. Almost cool and quirky even.

Importantly, it successfully captures the key to the iPhone’s success as a development platform: anyone with sufficient determination (yes, you!) can get through the unnecessary and bizarre horrors of Objective-C and the iPhone SDK, and can produce something that people might pay some money for. It’s something you can almost impress people with at parties. Almost.

Of course the book has an online forum and downloadable source code – de rigeur for coding books these days, but good to see nonetheless. In summary, if you want to try your hand at some simple iPhone apps, you really will need this book.


technorati

Technorati Profile

Drupal in perspective

Drupal prides itself on its community aspect. And so it should as a truly open source system. One of the good things to come out of this is the principle that dissenting voices should be given a platform. Hopefully what they say gets taken on board too. A talk titled ‘Why I hate Drupal’ at the recent DrupalCon in DC is a great example of this principle.

‘Why I hate Drupal’ was given by James Walker, one of the Lullabot team, and general Drupal insider and guru. It’s important to point out that this isn’t just some bile-filled sour-grapes rant. It’s a considered, insightful and eloquent discussion of some of the problems and issues with Drupal as it grows in popularity, and enters a new, more mature phase of its life.

Here’s a link to a video taken of the talk. It’s an hour long, so you may have to book out some time to watch it. It’s worth it though.

http://dc2009.drupalcon.org/session/why-i-hate-drupal

Drupal for education

Just noticed a Drupal for Education event going on at Sun’s offices in London, on 14th May:

http://www.drupal.org.uk/event/drupal-education-may-2009/14-may-2009

The event is organised by Codepositive, Sun, and Acquia, and seems to be part of a series of ‘Drupal for…’ events. The last one I went to was Drupal for Enterprise, and was pretty useful – especially for those who didn’t know anything about Drupal and wanted to get a good overview.

Interestingly, they mention on the event page that the Open University is now using Drupal for its new Platform site:

http://www.open.ac.uk/platform/

This bills itself as a ‘virtual campus’ social networking and news site for staff, students, and alumni. They even have blogs, although they’ve seemingly gone for Drupal-based blogs rather than the Wordpress MU we’ve chosen for blogs.kent.

Drupal: how to choose a CMS

All content management systems try to address one basic requirement: to allow people to build and maintain a website in a way that seems familiar and largely non-technical to them.

Of course, apparent simplicity so often belies considerable complexity. A system which “just” allows people to build a website in a simple way has to cater to a range of needs from a range of users.

The great challenge for a really good CMS is to provide all the things you’re likely to need to build a website, and – crucially – provide a means for adding in things later which you haven’t even thought of yet. This isn’t trivial.

Feature Creatures
When I first started looking at CMS I found it was easy to get obsessed with lists of features. Until, that is, I realized all my lists were basically the same. I ended up asking the same questions and getting the same answers. How dull.

It’s a bit like shopping for a new car and being told by the salesperson “Hey, this car has 4 wheels, a gearstick, and this amazing new thing called a ‘brake’, which lets you slow down really fast!”. “Oooo a brake?!”, you’d say. “Wow that’d be really useful”. You get the picture.

Admittedly this was just me being a bit naive. But it’s also because the CMS world is young, and it’s still possible for salespeople to get overly enthusiastic about features which are far from amazing. Most CMS companies can provide you with a very helpful checklist with all the features in their CMS ticked off. Seriously, how useful is that?

So, after two years looking at different CMS offerings I’ve found basically three things that truly differentiate CMS: cost, quality of support, and the potential for extending the system.

The best things in life are free
Some systems cost a lot of money. Really, they do cost a lot. For the cheaper ones we’re talking the cost of a rather fast car plus thousands a year in support bills. The middling ones might set you back a small house, and the really big systems will get you a yacht in the Med. In the current economic climate one seriously has to question the return on such an investment.

Some systems are free.

Drupal is open-source and free.

We chose Drupal.

So, the obvious question follows: did we choose Drupal just because it’s open-source and free?

Well, no. (But it helped.)

Will the support be any good?
Anyone starting out in Drupal has plenty of help. If you don’t have a budget at all, you can get by with your own nouse… oh and by the way, the help of some very bright and eager people all around the world.

If you do have some budget there are also plenty of companies offering expert help and development time. The great thing is that you’ve saved a whole load of cash by going for open-source and not paying some company a licence fee to start with. You can therefore focus what money you have on getting the system set up properly, and not worrying about annual ‘top up’ fees.

Luckily at Kent we have some budget, and plenty of bright people to work on adapting Drupal to our needs. An open-source Drupal therefore made perfect sense.

The future?
In fact Drupal is so powerful it’s not really a CMS. It’s a sort of a blueprint with which web developers can easily build novel components (modules, in Drupalese) that other people can then use too. Drupal just looks like a CMS because so many people have spent so much time developing with it that the end result is a pretty good CMS.

Given enough developers around the world, you can see how modules provide a vast scope for added functionality. If there’s something you really need in your system, the chances are that someone else somewhere in the world needed it too, and has already written a module for it.

One of the really attractive things about Drupal is the scope for us at Kent to contribute back to the Drupal community, so others can benefit from work we do. The more we help build up Drupal, the more other institutions might start to use it, so the more the system improves, and more people start to use it. It’s this community aspect to open-source software which is so powerful.

Large-scale
Finally, I’d like to mention a recent shift in perceptions of Drupal towards its being a scalable, enterprise-level CMS. Originally Drupal was viewed as something which could be used for small hobby websites. You know, the sort with a single contributor which generally nobody really cared too much about.

In the last couple of years more and more very big websites have started using Drupal. One that springs to mind is Red Nose Day 2009 (http://www.rednoseday.com). They had half a million visits in two days. If you’re telling me Drupal can’t cope with large-scale deployments, what you’re really telling me is your network and/or hardware can’t cope with large-scale deployments. Drupal can cope just fine.

To add to the sense of movement, a company called Acquia was recently set up by the founder of Drupal, Dries Buytaert. Its short-term aim seems to be to make money out of Drupal support, but in the long-term it does intend to help guide the progress of Drupal into the world of large-scale, enterprise-level deployments. Whether the company will succeed or not  - who knows. But the fact there there is a demand for Drupal to fill a large-scale CMS role is interesting.

Summary
Content mangement systems are numerous. Ultimately they all seem to offer very similar packages. At Kent University we’ve chosen Drupal because it’s open-source and free, and allows us great flexibility in how we build our system up. An added bonus is that we should be able to contribute our work back to the community, and benefit other like-minded institutions.