iPhone: screen rotates then app crashes - iphone

I want to get an instant screen's rotation when orienting it. In my ViewController there is one UIImage and one UILabel. I do as follows:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
}
Without the line app doesn't crash:
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
With the line of code trying to set duration to zero app crashes right after the rotation finishes.
Any idea what I'm doing wrong?
Thank you.

You are stacking many willRotateToInterfaceOrientation:duration: calls one after the other.
Infact first time the method is called, it will call it again, and again, and again,... until crash.
Putting duration:0 means nothing, infact the CoreAnimation timing used for the rotation effect is run on a separate thread than the Run Loop one so you cannot stop stacking calls in this way.
The reason why the app crashes after rotation probably is due to the fact that while the stack get filled your animation already started (separate thread).
You can get instant orientation by simply disabling autorotation and observing for the UIDeviceOrientationDidChangeNotification notification, then apply the appropriate view transform.

You're creating an infinite loop:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self willRotateToInterfaceOrientation:[self interfaceOrientation] duration:0];
}
You're calling the same function from within itself. What are you aiming to do?

Related

What/How to triggers the close-iris animation of UIImagePickerController?

I'm using a custom overlay view and showsCameraControls = NO. When I'm done I dismissModalViewControllerAnimated:YES. What appears to happen is that the iris appears fully closed (ie. no closing animation - just poof and it's closed) and then immediately slides down off the screen.
As a test I manually called viewWillDisappear on the UIImagePickerController and that makes the closed iris appear, but again no smooth animation.
I also tried wrapping the dismiss in a long animation transaction and that just made the re-appearence of the underlying navigation toolbar slow down. The iris behaved just as above.
I don't want to have to make my own iris animation - that would be uncool!
PS: Using sdk 4.0
To partially answer my own question, the best I have been able to come up with so far is:
- (void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker viewWillDisappear:YES];
[self performSelector:#selector(processPickerImage:)
withObject:[[info objectForKey:UIImagePickerControllerOriginalImage] retain]
afterDelay:0.1];
}
-(void) processPickerImage:(UIImage *)uiImage
{
// do stuff
[self dismissModalViewControllerAnimated:YES];
// dismiss your custom overlay etc.
[uiImage release];
}
It doesn't actually animate the iris, but at least it is onscreen immediately so the user recognises that the photo taking is done. I'm also not super happy that viewWillDisappear gets called twice on the UIImagePickerController - I'm not sure it's guaranteed to be safe.
Also the status bar appears over the iris which is annoying.
I'm hoping someone else has a better solution?

Get launch orientation of iPad app

In my iPad app, I need to run some layout code to set the proper layout depending on the orientation. By default, the layout is configured for the landscape orientation, so in the case that the app starts in portrait mode, I need to take extra action to configure the views properly for display in portrait.
In my -application:didFinishLaunchingWithOptions: method, I check the orientation using [[UIDevice currentDevice] orientation]. The problem here is that it always returns portrait even if the app is starting in landscape. Is there any way around this?
This is expected behavior. Quoth the UIViewController documentation:
Note: At launch time, applications should always set up their interface in a portrait orientation. After the application:didFinishLaunchingWithOptions: method returns, the application uses the view controller rotation mechanism described above to rotate the views to the appropriate orientation prior to showing the window.
In other words, as far as the device is concerned the orientation is portrait while the application is launching. At some point after application:didFinishLaunchingWithOptions: it will detect the different orientation and call your shouldAutorotateToInterfaceOrientation: method and then your other view rotation methods, which you should handle as normal.
This is the best way to check for orientation on launch. First, create a new method in your AppDelegate that checks the orientation:
-(void)checkLaunchOrientation:(id)sender{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL isLandscape = UIDeviceOrientationIsLandscape(self.viewController.interfaceOrientation);
if (UIInterfaceOrientationIsLandscape(orientation) || isLandscape) {
//do stuff here
}
}
At the end of -application:didFinishLaunchingWithOptions: run
[self performSelectorOnMainThread:#selector(checkLaunchOrientation:) withObject:nil waitUntilDone:NO];
Use self.interfaceOrientation in your view controller - it's a property of UIViewController that is set by iOS for you, and in some cases is more reliable than [[UIDevice currentDevice] orientation].
Here's a detailed description: http://bynomial.com/blog/?p=25
As mentioned in a blog post above, there is a set of macros for testing orientation. That blog post however mentions UIDeviceOrientationIsPortrait. I like the following below, it's a minor twist.
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
NSLog(#"Portrait");
}
else
{
NSLog(#"Landscape");
}
An observation I've made is that you can't call this code in a table view, pushed on to a Navigation Controller embedded in the split view controller. So in other words you can't call it from the master view controller. You have to replace the "self.interfaceOrientation" with splitviewcontroller.interfaceOrientation, assuming you maintain a reference to the parent split view controller.
Use the status bar orientation instead to detect it.
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
then perform the if's on the "orientation" you have obtained from above.
So the question is about checking orientation at startup. The answer is sadly "You can't".
But AFTER startup, you can check orientation the normal way (as others have described).
If anyone else comes here looking for an answer, simply stop looking since, at startup the orientation variable is not set (all views frames/bounds also report being in portrait even if they aren't).
You want to make sure you set the proper keys in your Info.plist to allow for the orientations you want:
UISupportedInterfaceOrientations
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
Not that you need another answer, but I thought I should add that you almost never want to use [[UIDevice currentDevice] orientation]. That method returns the orientation of the device, which isn't necessarily the same as the orientation of the interface.
It's not true that you can't figure out the launch orientation, it is true that it's a pain in the rear to do so.
here's what you need to do.
your first UIViewController needs to have some special logic to nab the information you'd like.
you might even want to create a UIStartupController just for these purposes if it's that important to your flow.
in the case of my project, we already had such a startup controller present.
all you need is the following code
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.launchOrientation = UIDeviceOrientationUnknown;
}
return self;
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (self.launchOrientation == UIDeviceOrientationUnknown && duration > 0)
self.launchOrientation = UIInterfaceOrientationPortrait;
else
self.launchOrientation = toInterfaceOrientation;
}
basically, if we're not launching in UIInterfaceOrientationPortrait, the first rotation callback sequence will actually reveal the launch orientation.
if launched in UIInterfaceOrientationPortrait, then we need to check that the first rotation's duration is non zero, and then we know that it was launched from portrait.

iphone opengl backing becomes w=0 h=0 after MFMessageComposeViewController

I have an opengl game for iPhone/iPad (universal). I added the ability to send an SMS message using MFMessageComposeViewController. Testing in a real iPhone. The SMS composer sheet animates up over my view, I can send the message or not, didFinishWithResult gets called, and when I [self dismissModalViewControllerAnimated:YES] it goes away and my glview is asked to layoutSubviews. At that point the backing width and height are now zero, and my frame buffer status check fails. The self.layer.frame.size.width is still 320x460.
- (void)layoutSubviews
{
NSLog(#"layoutSubviews");
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
[self drawView];
}
I do have a UIViewController for my glView which is where I handle orientation changes for the iPad and where I also put the MFMessage stuff. (Technically I guess since it's universal there are two different viewControllers, two app Delegates and two nibs - but I'm working in the iPhone set here because the iPad doesn't sms). On the iPad layoutSubviews gets called when the orientation changes, we destroy and re-create the framebuffers at the new size and everything is fine. But here when coming back from sending the SMS it fails on the re-creating. I can post the code if necessary but its the standard creating framebuffer code.
Another important point is that I'm using a notification to tell the method inside of the viewcontroller to start the sms stuff. I tried just having those methods in my glview and making it the MFMessageComposeViewControllerDelegate but then I was getting errors because glview is a UIView and not a UIViewController.
Any ideas?
Not sure if it's a bug or what the deal is but I had to create another view, make self.view = anotherView, retain my glview and removeFromSuperview before presenting the modal. And then wait to bring my glview back until everything was animated back into place.
If anyone wants more info please let me know.
Edit with actual answer:
It is a bug and as I suspected it has to do with the status bar. My app has no status bar. But when I
[self presentModalViewController:controller animated:YES];
the SMS message composer view does show the iphone status bar. When it is dismissed and my app showed through underneath my framebuffer was getting borked. I had figured out a weird work around of switching views to protect my glview framebuffer - but then figured out to add a statusBarHidden before the dismiss and all is well now. Here's the dismiss code:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result {
// Notifies users about errors associated with the interface
switch (result)
{ ... }
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[self dismissModalViewControllerAnimated:YES];
}

iPhone OS: Rotate just the button images, not the views

I am developing an iPad application which is basically a big drawing canvas with a couple of button at the side. (Doesn't sound very original, does it? :P)
No matter how the user holds the device, the canvas should remain in place and should not be rotated. The simplest way to achieve this would be to support just one orientation.
However, I would like the images on the buttons to rotate (like in the iPhone camera app) when the device is rotated. UIPopoverControllers should also use the users current orientation (and not appear sideways).
What is the best way to achieve this?
(I figured I could rotate the canvas back into place with an affineTransform, but I don't think it is ideal.)
Thanks in advance!
Just spouting off an idea (not sure if it would work or not)...
Perhaps you could have your screen controlled by a UIViewController that supports all orientations, but have the canvas be controlled by one that only supports a single orientation (ie, returns NO in its shouldAutorotate... method).
If that doesn't work, I'd probably just go with the affineTransform route.
I discovered a way to do this, similar to what Dave DeLong proposed.
Using a transform worked, but it wasn't ideal. Although the end result (end of the animation) was what I wanted, it would stay show some kind of shaky rotation animation.
Then I found this:
https://developer.apple.com/iphone/library/qa/qa2010/qa1688.html
Which says that a second (or third etc.) UIViewController added to the WINDOW would not receive rotation events, and therefore would never rotate. And that worked!
I created a 'fake' UIViewController with a blank view and added that as the first view controller. This receives the rotation events which I then pass on to the other view controllers that can then choose whether to rotate - the entire view or just button labels.
It is a bit hacky... But I guess the user won't notice.
Using the willRotateToInterfaceOrientation method, you should be able to use some simple logic to swap out the images:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//The following if statement determines if it is an iPad, if it is then the interface orientation is allowed. This line can be taken out to support both iPhones an iPads.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Landscape Right");
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Landscape Left");
} else {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Portrait");
}
} else {
}
}
To change an image in your interface programmatically:
[myUIImageView setImage:[UIImage imageNamed:#"myImage.png"]];
Also, to make sure the view doesn't auto-rotate use the shouldAutorotateToInterfaceOrientation method to tell your app to stay in portrait.

Hide Shutter in UIImagePickerController

I have designed an iris shutter animation for a camera view in an iPhone app.
Unfortunately, it seems impossible to hide Apple's shutter when the view appears, even if I hide the camera controls and create a custom cameraOverlayView.
I have gotten around this by animating my shutter on top of the normal shutter when the view appears, using the viewWillAppear and viewDidAppear methods of UIImagePickerController. However, I can't get the shutter to be hidden under my shutter the first time through. When the app launches, it shows a camera view, and the original shutter is visible. On all subsequent views of the cameraController, my workaround works. Any suggestions?
Here's my code. This is from my app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
cameraController = [[CameraController alloc] initWithMode:#"camera"];
[window addSubview:cameraController.view];
}
And this is from my UIImagePickerController subclass:
- (void) viewWillAppear:(BOOL)animated {
if (self.sourceType != UIImagePickerControllerSourceTypePhotoLibrary || simulatorView) {
[self addShutter];
[shutter close];
}
[super viewWillAppear:animated];
}
- (void) viewDidAppear:(BOOL)animated {
if (self.sourceType != UIImagePickerControllerSourceTypePhotoLibrary || simulatorView) {
[shutter openShutter:.5f];
}
[super viewDidAppear:animated];
}
Note that the docs say that subclassing UIImagePickerController isn't supported, so it may work in some cases but isn't "safe". Not sure if it would get rejected by the app store. (Probably depends on how picky their static code verification tool is.)
I don't really have a good answer, but you might try either 1) iterating over the subviews of the picker's main view to see if you can identify whatever is being used to animate the shutter, then mangle it so that it won't display, or 2) for the initial animation, just show the initial image picker main view under another opaque black view. Not sure if the user-specified overlay view would work for that or not, but you might be able to do those without subclassing.
Searching for undocumented subviews is another thing that's theoretically unsafe though since who knows how the implementation might change in the future.
Possibly too late, but my proposal is to use the following notifications (found while debugging)
PLCameraControllerAvailable - camera controller object is initiated, but shutter is not visible yet
PLCameraViewIrisAnimationDidEndNotification - iris animation is completed.
And the usage is straightforward: call UIGetScreenImage() on 1st notification, render grabbed image on screen (fullscreen) just above the UIImagePicker. Destroy rendered image on the 2nd notification.
I try the same thing with no results, so I do this workaround:
1- Suppose you have a method called showAllButtons with no parameters that will show all your custom things (buttons, tool bars,...)
2- Initialize all the custom controls hidden
3- Write a method that will call the last function but within an interval:
-(void)showAllButtonsDelayed:(NSTimeInterval)a_iMsToDelay
{
NSTimer* tmpShowButtonsTimer = [NSTimer timerWithTimeInterval:a_iMsToDelay target:self selector:#selector(showAllButtons) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:tmpShowButtonsTimer forMode:NSDefaultRunLoopMode];
}
4- Call that method in the willDidAppear method of the UIImagePickerController subclass. Play with some values of a_iMsToDelay.
Hope this helps.