Force landscape orientation in UIImagePickerController
As per the UIImagePickerController Class Reference
Important: The
UIImagePickerController
class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.
So it looks like forcing landscape mode is unsupported and discouraged, unless you do so by replacing the default controls with a custom cameraOverlayView
.
Using UIImagePickerController in landscape orientation
If you'd like to use UIImagePickerController in landscape mode, use user1673099's answer, but instead of:
- (BOOL)shouldAutorotate
{
return NO;
}
use:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
and then the picker would open in landscape mode:
But make sure you check Portrait in deployment info:
Force UIImagePickerController to take photo in portrait orientation/dimensions iOS
The imageOrientation flag is set on the UIImage depending on which way up the camera is being held when the picture is taken. This is a read-only flag and determines the orientation that iOS will attempt to display the image when you place it into a view.
You could check the orientation before displaying the image, and manipulate the imageView accordingly. Assuming you have an imageView enclosed inside a same-sized view...
- (void) imagePickerImage:(UIImage*)image
{
if (image.imageOrientation == UIImageOrientationUp ) {
self.imageView.bounds = CGRectMake
(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
self.imageView.transform = CGAffineTransformMakeRotation(M_PI/2);
} else if (image.imageOrientation == UIImageOrientationDown) {
self.imageView.bounds = CGRectMake
(0, 0, self.view.bounds.size.height, self.view.bounds.size.width);
self.imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
}
else {
self.imageView.bounds = self.view.bounds;
self.imageView.transform = CGAffineTransformIdentity;
}
self.imageView.image = image;
}
Another option could be to create a new image from an imageRep: this should strip out the orientation information.
update
The native pixel dimensions of an iPhone photograph are landscape, regardless of the way the phone is held when it is taken. It is only the orientation EXIF flag that is set differently. You cannot alter the EXIF flag but you can strip it out by copying the image bitmap data to a new bitmap image. See also my answers here.
Here is a way to strip out the EXIF data and add in a portrait EXIF flag. You are creating a new UIImage and setting the EXIF flag as you create it. You could use this to flag your landscape images as portrait (UIImageOrientationRight
).
- (UIImage*)stripExif:(UIImage*)image
{
CGImageRef cgImage = image.CGImage;
UIImage* result = [UIImage imageWithCGImage:cgImage scale:1
orientation:UIImageOrientationRight];
return result;
}
update 2
It is misleading to refer to the imageOrientation property of UIImage as an EXIF flag, for two reasons:
(1) image metadata is stored in a dictionary-style structure, within which can be a number of sub-dictionaries, one of which is EXIF. Others include TIFF, IPTC, PNG for example. There is also a general set of tags at the top-level dictionary. The orientation key is NOT in the EXIF section, it is found in the TIFF and IPTC subdictionaries (if present) and also as a top-level key. We tend to use the term EXIF when referring to image metadata as a whole but this is misleading.
(2) Orientation metadata in the image file uses a different set of flag conventions to those that Apple uses in their UIImage.imageOrientation property. Therefore you have to take care if reading this property in order to display the image correctly. Apple evidently uses the image file's orientation metadata to set the UIImage's property, but there is a bit of translation going on.
Orientation
Apple/UIImage.imageOrientation Jpeg/File kCGImagePropertyOrientation
UIImageOrientationUp = 0 = Landscape left = 1
UIImageOrientationDown = 1 = Landscape right = 3
UIImageOrientationLeft = 2 = Portrait down = 8
UIImageOrientationRight = 3 = Portrait up = 6
Here is a page showing 'exif' rotation flags. Compare with Apple's UIImage documentation (constants section). See also Apple's CGImageProperties_Reference, especially kCGImagePropertyOrientation
I have loaded a small project onto github logging various UIImage metadata issues
UIImagePickerController in Landscape
I haven't checked whether this is illegal, but it worked for me.
If you want the UIImagePickerController to start(and stay) in Landscape orientation code:
//Initialize picker
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
//Refer to the method didRotate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
//Set the picker source as the camera
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Bring in the picker view
[self presentModalViewController:picker animated:YES];
The method didRotate:
- (void) didRotate:(NSNotification *)notification
{
//Maintain the camera in Landscape orientation
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
Cannot get landscape with portrait mode for image picker to work
I had a similar problem a while back. If I understand you question correctly, you want the app to stay in landscape mode unless the photo picker is presented. Here is what you have to do:
- Go into your settings and make these three orientation options are checked: Portrait, Landscape Left, Landscape Right.
Go into your AppDelegate and add this static variable:
static var canRotate = false {
didSet{
UIDevice.current.setValue(canRotate ? UIInterfaceOrientation.portrait.rawValue : UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
}
}Also in your AppDelegate, add this function:
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if AppDelegate.canRotate {
return .portrait
} else {
//return the orientation you want your app to be in
return [.landscapeLeft, .landscapeRight]
}
}
At this point, you can control the whether or not you can rotate the device by calling AppDelegate.canRotate = true or false
All that is left is to add this variable in your image picker code, here's what you do:
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
AppDelegate.canRotate = true
self.present(imagePicker, animated: true, completion: nil)
}
Also, in this function:
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
AppDelegate.canRotate = false
picker.dismiss(animated: true, completion: nil)
}
And in this one as well:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
AppDelegate.canRotate = false
picker.dismiss(animated: true, completion: nil)
}
There may be optimizations for this code but this is the core functionality you'll need.
Let me know how this goes for you, if it works, glitches, or what not.
Related Topics
How to Add Background Image on iPhone Navigation Bar
Upload Image with Multipart Form-Data iOS in Swift
How to Implement Pageview in Swiftui
Phonegap Cdvviewcontroller.H File Not Found When Archiving for iOS
Core Animation Progress Callback
Ios9 Does Not Load Insecure Resources from a Secure Page (Ssl/Https)
What's the Difference Between All the Selection Segues
Automatic Preferred Max Layout Width Is Not Available on iOS Versions Prior to 8.0
Error When Adding Input View to Textfield iOS 8
Swift Different Images For Annotation
Delete All Keys from a Nsuserdefaults Dictionary iOS
Exhaustive Condition of Switch Case in Swift
How to Get the Console Logs from the iOS Simulator
Uitextview That Expands to Text Using Auto Layout
Use Multiple Font Colors in a Single Label