Thursday, March 4, 2010

How To Use UIImagePickerController In Landscape App




My app just got rejected for displaying UIImagePickerController in the landscape mode. As Apple doc stated, UIImagePickerController (will be refered as UIIPC from now on) only works in portrait mode. For SDK 3.0+, this is no problem since i think it will auto display in portrait mode (correct me if im wrong). But for me, who is using 2.2.1 still, becomes a problem. I know there are quite a few posts about this issue with solutions but I tried all those and proves to be either "not working" or "too difficult to implement.". Especially those that suggest to apply transform to all the views object.



The problem is that the UIIPC launched in landscape mode causes the thumbnails to be stretched in devices, and appears funny in simulator. Such as below:


After few hours of banging my head on my MacBook, I came up with a simple hack that I think works ok. Here's one way how to solve this problem - very easy way without needing to apply transform to all of your objects and views. This is a sample app i created. From launch, the app starts from landscape.  

Now when I press a button to load a photo (calling UIIPC - either savedphotosalbum/photolib), the app display a "transition view" that halts the operation until the user rotates the device. Put a message/text on it to ask the user to rotate the device to proceed. At the time of this view displaying, also set a GLOBAL BOOL FLAG, (named WANTSPORTRAIT for me), to YES. (initially set to NO)

Then, when the user rotates it, detect the orientation (by the delegate function of  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation). In this delegate function, detect the flag and if it is set, call the UIIPC and return UIInterfaceOrientationPortrait. Voila, now your UIIPC is in Portrait.

The next thing to do is to handle the selected image/photo. Once the image/photo is selected, the appropriate delegate function will be called (for 4.0, it is - - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info. In this function set WANTSPORTRAIT to NO and again, display the transition view with a message asking the user to rotate to landscape. (You can customize these messages, and even easily apply simple transform on them to make it appear nice). 

And when the user rotates it again back to landscape, the delegate detects it and return the orientation back to landscape. The project sample probably is not perfect (eg if user rotates the other way or etc, you need to modify it to handle all of orientation)

This method is accepted by Apple Reviewer. 2 of my apps are using this method and appears to be accepted by users.