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.

8 comments:

  1. what if you just want to use the camera to take photos in landscape mode?

    ReplyDelete
  2. How 2 create a custom slider programatically...........

    ReplyDelete
  3. I done Twitter application for iphone..everithing going good bt TweetMessage is not posted....Plz....help me

    ReplyDelete
  4. This issue is not a problem anymore since IOS4 everything is peachy - the UIImagePickerController will automatically shown in portrait mode nicely even when your app is landscape.

    ReplyDelete
  5. Nice Post!! LiveWebTutors is the preferred service for offering essay writing service . The service solutions are blessed with a team of diligent writers, researchers and editors to provide you with the right assistance within the perfect timeline.

    ReplyDelete
  6. It is a stay at home service which makes it extremely easy to access Assignment Help whenever in need. The professionals are more than ready to adopt all the demands and criteria of the students regarding their assignments. Having someone to guide us throughout the assignments improves our skills in unimaginable ways.

    ReplyDelete
  7. Ignousynopsis is a website dedicated to providing Ignou projects, Phd thesis and solved assignments at a very affordable price to all the learners across all genres. Our writers offer free consultancy regarding the Ignou MARd project topic selection to the students. If you need any help in composing Ignou synopsis and Ignou MARD solved assignment 2020 21 look nowhere else, and count on us.
    Ignou DNHE 4 Project and Course Details
    Smart Idea to get Ignou Project Help
    Ignou MA Psychology Project Help
    Ignou MBA Project Writing Assistance in India

    ReplyDelete