Objective-c – UIImageView rotate and resize

iphoneobjective c

I want to rotate my UIImageView and set the size according to the aspect ratio.
But I have some problems with that. First I paste here my code, then I explain the problem.

imageView.size = CGSizeMake(myWidth, myHeight);
[imageView setContentMode:UIViewContentModeCenter];

So these two lines set the size and the contentMode of my imageView.
When I do this the image appears correctly. So when the device is rotated I catch the rotation notification and

if ([self isLandscape]) {
    CGFloat aspect = myHeight / myWidth;
    imageView.size = CGSizeMake(myWidth/aspect, myWidth);
    [self rotateView:imageView withDegree:degree];
    imageView.origin = CGPointMake(x, y);
} else {
    [self rotateView:imageView withDegree:degree];
    imageView.origin = CGPointMake(x, y);
    imageView.size = CGSizeMake(myWidth, myHeight);
}

So then the rotation is done. But you can see that I am also do a resizing.
The problem is that my imageView has a minimum size which cannot be overridden.
But I figured out that if I change the contentMode property of my imageView from UIViewContentModeCenter to UIViewContentModeScaleAspectFit then it will work good but the size is smaller than what I expect. Why is that? 🙁 How can I solve this? Please if you have any help!

Best Answer

ScaleAspectFit will only scale up the content so that it is all visible and no clipping occurs. If you want your content to fill the view and possibly have some clipping, you should use UIViewContentModeScaleAspectFill instead.

However, if all you want to do is enforce a minimum size for each dimension, you could use the MAX macro instead.

CGSize mySize = CGSizeMake(myWidth, myHeight);
mySize.x = MAX(mySize.x, myXMin);
mySize.y = MAX(mySize.y, myYMin);
imageView.size = mySize;

Also, just checking that you really meant for landscape size to be (width*width/height, width) and not just (height, width).