Tag Archives: Twitmo

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…