Using Uiimagepickercontroller in Landscape Orientation

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:

Sample Image

But make sure you check Portrait in deployment info:

Sample Image

Use UIImagePickerController in landscape mode in Swift 2.0

It absolutely supports landscape mode. Put this extension somewhere. Best in a file named UIImagePickerController+SupportedOrientations.swift

extension UIImagePickerController
{
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
}

This makes all UIImagePickerControllers in your app landscape. You can also subclass it and override this method to make only a subclass landscape-able:

class LandscapePickerController: UIImagePickerController
{
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
}

Finally, to support all orientations you can return

return [.Landscape, .Portrait]

For Swift 3:

    extension UIImagePickerController
{
override open var shouldAutorotate: Bool {
return true
}
override open var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .all
}
}

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.

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];

}

UIImagePickerController: Check if returned image is in landscape or portrait?

I used this to check and fix orientation if u need to fix it.

switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, imageSize.width, imageSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;

case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, imageSize.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;

case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, imageSize.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
/////////////// or u just do it in this way:
if (image.imageOrientation == UIImageOrientationUp) {
NSLog(@"portrait");
} else if (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) {
NSLog(@"landscape");
}

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:

  1. Go into your settings and make these three orientation options are checked: Portrait, Landscape Left, Landscape Right.
  2. 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")
    }
    }
  3. 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.

UIImagePickerController landscape mode shifts screen

It seems the only viable solution is portrait-locking the whole application.

Lesson learned: Swift-UI is absolute garbage.

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



Related Topics



Leave a reply



Submit