I have an app that works on landscape, already in the store, and decided to add a camera for a certain new feature in my next update.
with an iPad 2 the image inside the camera appears rotated 90 degrees in clockwise direction.
Once the picture is taken it appears it's saved correct position.
Needed is that always there should be a landscape mode,while taking picture and once image is clicked and saved.But image inside the camera appears to be in Portrait view. I also checked the device orientation i.e faceUp orientation.
I have used the below code to manage the orientation but could not resolve
- (BOOL)shouldAutorotate {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
// UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationFaceUp )
{
return NO;
}
else if (orientation == UIDeviceOrientationFaceDown)
{
return NO;
}
return YES;
}
Related
I need to set my camera orientation to landscape in iOS 9; my code works in iOS 7 and 8 but does not seem to work now.
In iOS 8, I call CameraViewController : UIImagePickerController, with setting ShouldAutoRotate >> return NO.
I have already tried this:
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:#"orientation"];
}
else
{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:#"orientation"];
}
There is no change in UIDeviceOrientationLandscapeRight, but if UIDeviceOrientationLandscapeLeft, it's rotated 90 degrees but the size is not correct.
In the info.plist I have set it to only accept landscape orientation. What else should I do or might have missed?
I am working in an OpenGL ES game, c++ and iOS. I would like that the canvas axes be always as in default portait orientation, and also be able to know which is the initial device orientation and all the orientation changes.
The problem I have is that if I configure the XCode project to support the 4 orientations: 2 Portraits and 2 Landscapes, then the Frame which is embedded the OpenGL canvas has the axes depending the initial orientation. If I configure the project to support only Portrait Orientation, then the Application won't tell me if it is in Landscape.
I would like to find a way to do this as much programmatically as possible.
1) To Know programatically in iOS
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft)
landscapeRight = false;
else if(orientation == UIDeviceOrientationLandscapeRight)
else if(orientation == UIDeviceOrientationPortraitUpsideDown)
else if(orientation == UIDeviceOrientationPortrait)
....
2) For Default portrait view
Restrict other orientations in your Xcode project's info.plist
My Storybuilder is designed with a portrait layout. When I start the app with my iPad already turned to horizontal, it's able to correctly detect it's in a horizontal position. But when I start the app with my iPad in a portrait position, it thinks it's in horizontal. However, every time I rotate it, the code is able to detect the correct orientation properly.
- (void) viewDidLoad
{
[self updateForOrientation];
}
- (void)updateForOrientation
{
if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) // became portrait
{
NSLog(#"is portrait");
//code for changing layout to portrait position
}
else //became horiztontal
{
NSLog(#"is horizontal");
//code for changing layout to horizontal position
}
}
Output: is horizontal (this is the output whether it starts up as portrait or landscape)
The problem is that you're sending the devices orientation in terms of the UIDeviceOrientation enum to a function that's expecting a UIInterfaceOrientation value.
If you command click on UIInterfaceOrientationIsPortrait(), you can see that it is defined as follows.
#define UIInterfaceOrientationIsPortrait(orientation) ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)
And if you look at the enum declarations for the two orientation types (documentation links below), you can see that there is a misalignment in value due to the device orientation containing a value for "none". Anyway, changing your code to use UIInterfaceOrientation should sort this out. Example:
- (void)updateForOrientation
{
UIInterfaceOrientation currentOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
NSLog(#"is portrait");
}else{
NSLog(#"is horizontal");
}
}
https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIInterfaceOrientation
https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/doc/c_ref/UIDeviceOrientation
I'm using AVFoundationFramework to capture video. When I starting capturing I set video orientation based on current device orientation. I do it like this:
startRecordingOrientation = [[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation avcaptureOrientation;
if ( startRecordingOrientation == UIDeviceOrientationLandscapeLeft )
{
avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight;
}
else if ( startRecordingOrientation == UIDeviceOrientationLandscapeRight )
{
avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft;
}
else if(startRecordingOrientation == UIDeviceOrientationPortrait)
{
avcaptureOrientation = AVCaptureVideoOrientationPortrait;
}
else if(startRecordingOrientation == UIDeviceOrientationPortraitUpsideDown)
{
avcaptureOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
}
[videoConnection setVideoOrientation:avcaptureOrientation];
But I have problem with UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown. I need to know what is device oriention to properly set recording video orientation becouse If I don't do that my video output will be rotated. I can't use InterfaceOrientation instead of AVCaptureVideoOrientation becouse my application use only portrait orientation. Is there any way to determine if iOS device is in exactly portrait/landscape orientation? If not, I can load my recorded video and then determine video orientation and rotate it to portrait but is there any way to determine orientation of already recorded video?
UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown haven't a corresponding interface rotation, by default you can match the orientation that you prefer, I use portrait.
I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode
- (void)viewDidAppear:(BOOL)animated
{
UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
NSLog(#"orientation %d", orientation);
if ((orientation == 2) || (orientation == 3) || (orientation == 4))
{
[self performSegueWithIdentifier:#"DisplayLandscapeView" sender:self];
isShowingLandscapeView = YES;
}
}
The interfaceOrientation property is deprecated since iOS 8.
And the helpers methods
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
won't help either, because they return false when orientation is .faceUp.
So I ended checking this way:
extension UIViewController {
var isPortrait: Bool {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait, .portraitUpsideDown:
return true
case .landscapeLeft, .landscapeRight:
return false
default: // unknown or faceUp or faceDown
guard let window = self.view.window else { return false }
return window.frame.size.width < window.frame.size.height
}
}
}
This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.
I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.
In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.
The most important difference for your case is that the interface orientation is never face up/down.
In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.
Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)
Bear in mind that a device orientation landscape left means a user interface orientation landscape right.
Yes you can, the UIDeviceOrientation is an enum which contains:
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
There are even two helpers:
UIDeviceOrientationIsPortrait(orientation)
UIDeviceOrientationIsLandscape(orientation)
Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.
There is one way to check it.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.