I have a problem where I need to rotate an UIImageView and resize depending if it's Portraite mode or Landscape mode.
I've managed to get the image view to rotate correctly, by doing as the answer in this related problem says: Rotate UIImageView Depending on iPhone Orientation.
This is my code for the rotation (for now I've only focused on rotating it to left landscape):
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
float angle = DEGREES_TO_RADIANS(degrees);
CGAffineTransform transform = CGAffineTransformRotate(
CGAffineTransformMakeTranslation(
160.0f-self.view.center.x,
240.0f-self.view.center.y
),angle);
image.transform = transform;
[UIView commitAnimations];
}
How I initiate the UIImageView:
UIImage *image = [UIImage imageNamed:@"picture.png"];
self.testImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
self.testImageView.image = image;
self.testImageView.contentMode = UIViewContentModeScaleAspectFit;
self.testImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
What's happening now is that the Image gets rotated but has the same size as in portrait mode in landscape mode, but is centered (that I understand why, as I did the rotation as the answer to the linked question).
So, what I want is that the image is viewed in fullscreen, as the original image has the scale to fit in fullscreen landscape. As I've set
UIViewContentModeScaleAspectFit
my original naive thought that this would magically rescale to fit the proper landscape scale. That's obviously not the case. Any hints?
Try using UIViewContentModeScaleAspectFit and set in your parent view property autoresizesSubviews to YES. Image should resize automagically.