Doing Battle with imagePickerController and Orientation

I am in the home straight – hopefully – with my new app which is, unsurprisingly enough, photography related. It’s an order of magnitude of complexity above the weight tracker, and has been a real challenge to get through.

The interface to the  ALAssetsLibrary isn’t without its foibles, including the fact that, when used in conjunction with the image picker, there is a fair amount of work to be done by hand.

One of the bigger surprises is image orientation, which you have to add yourself.

Perhaps this incantation may come in handy for you:

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
int imOrNum = image.imageOrientation;
...
int orientMetaVal = 0;
    if (imOrNum == 1)
    {
    // landscape, vol buttons up
        orientMetaVal = 3;
        landscapeSnap = TRUE;
    }
    else if (imOrNum == 3)
        //portrait, home button down
        orientMetaVal = 6;
    else if (imOrNum ==0)
    {
        // landscape, vol buttons down
        orientMetaVal = 0;
        landscapeSnap = TRUE;
    }
    else if (imOrNum == 2)
    {
        // portrait, home button up
        // 5 is weird - thumbnail right, image inverted.
        orientMetaVal = 8;
    }
...
[tmpMetadataDic setObject:[NSNumber numberWithInt:orientMetaVal] forKey:@"Orientation"];
So the landscape trueness is just something that I use when I’m previewing the image that has just been taken. You then need to set various bits and pieces in a NSMutableDictionary, such as the orientation, which eventually gets written back with the file as a parameter to
[yourInstantiatedAl writeImageToSavedPhotosAlbum:[nicePicture CGImage]
                            metadata:tmpMetadataDic

One other thing to look out for is the requirement to add in all of the vanilla EXIF data – ISO, shutter, etc. That’s actually pretty easy. I initialise the dictionary of metadata with this:

NSMutableDictionary *tmpMetadataDic = [info objectForKey:UIImagePickerControllerMediaMetadata];