Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate UIImageView Depending on iPhone Orientation

How would I do this, all I want to do is rotate a UIImageView depending on the orientation of the iPhone.

like image 960
Joshua Avatar asked Jul 31 '10 10:07

Joshua


People also ask

Can you rotate multiple photos at once iPhone?

Now, to batch rotate images on your iPhone, just head to the Shortcuts app and tap on your Rotate Images shortcut. The shortcut automatically brings up your photo album. You just need to select the images you want to rotate and tap Add.

Why are my photos downloading sideways?

Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.

What is EXIF orientation?

The EXIF orientation value is used by Photoshop and other photo editing software to automatically rotate photos, saving you a manual task.


1 Answers

You can do this through IB, to get an app with a portrait and a landscape layout, or you can do it programmatically. This is about the programmatic way.

To get notifications on the change of orientation, use

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(orientationChanged)
                name:UIDeviceOrientationDidChangeNotification
                object:nil];

and add a function like this (note this is copypasted from a project and a lot of lines are left out, you will need to tune the transformation to your specific situation)

-(void)orientationChanged
{
    UIDeviceOrientation o = [UIDevice currentDevice].orientation;

    CGFloat angle = 0;
    if ( o == UIDeviceOrientationLandscapeLeft ) angle = M_PI_2;
    else if ( o == UIDeviceOrientationLandscapeRight ) angle = -M_PI_2;
    else if ( o == UIDeviceOrientationPortraitUpsideDown ) angle = M_PI;

    [UIView beginAnimations:@"rotate" context:nil];
    [UIView setAnimationDuration:0.7];
    self.rotateView.transform = CGAffineTransformRotate(
                                CGAffineTransformMakeTranslation(
                                    160.0f-self.rotateView.center.x,
                                    240.0f-self.rotateView.center.y
                                ),angle);
    [UIView commitAnimations];
}

When you're done, stop the notifications like so:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
like image 168
mvds Avatar answered Sep 19 '22 10:09

mvds