Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

orientation - Crash on presenting UIImagePickerController under iOS 6.0

My app only supports landscape orientations via the supportedInterfaceOrientation properties.

Using an iOS prior to iOS 6, my app can successfully load an instance of UIImagePickerController via presentViewController:animated:completion: even though the UIImagePickerController itself only supports portrait orientation.

The image picker simply presented itself sideways to the user. The user rotated the phone, picked their image, and then rotated back to landscape.

Under iOS 6.0, calling presentViewController:animated:completion: with the UIImagePickerController instance crashes the app. I can prevent the crash by adding portrait options to my supportedInterfaceOrientation properties.

However, operating in portrait really does not make sense for my app. I had thought I could use shouldAutorotateToInterfaceOrientation to allow the app to "support portrait" but only be allowed to rotate to portrait in this one view. But now that method is deprecated, and I can't use the same technique with shouldAutorotate.

Does anyone have any ideas how I can get around this issue under iOS 6.0?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

iOS 6.1 - fixed

As of iOS 6.1, this no longer occurs, it is very important to follow my tips in order to avoid a crash under iOS 6.0.x, the below still applies to that.


iOS 6.0.x workaround

This is in actual fact a bug in iOS 6.0, this should be fixed in future iOS releases.

An engineer from Apple has explained this bug and a workaround here: https://devforums.apple.com/message/731764

This is happening because the Application wants landscape orientation only but some Cocoa Touch View Controllers require strictly Portrait orientation which is the error - not that they should be requiring more then Portrait but their interpretation of the Applications requirements.

An example of this can be the following:

iPad app supporting landscape only displays a UIImagePickerController via a UIPopoverController. The UIImagePickerController requires Portrait orientation, but the app is forcing landscape only. Error and... crash

Other frameworks that have been reported as problematic include the Game Center login view controller.

The workaround is pretty simple but not ideal... You keep the correct orientations declared in your info.plist/project info pane, but in the Application Delegate class you declare that you allow all orientations.

Now each View Controller you add to the window must specify itself that it can only be Landscape. Please check the link for more details.


I cannot stress how much you should not be subclassing UIImagePickerController as the accepted solution is insisting you do.

enter image description here

The important thing here is "This class is intended to be used as-is and does not support subclassing."


In my case I added this to my application's delegate (I have a landscape only app), this tells the image picker it can display, because portrait is supported:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

And then in my view controller which happened to be a UINavigationController, I included a category with the following:

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

Now my app doesn't rotate, and the image picker asks the delegate if it can display as portrait and it gets told that's okay. So all plays out well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...