简体   繁体   English

UIImageView旋转并调整大小

[英]UIImageView rotate and resize

I want to rotate my UIImageView and set the size according to the aspect ratio. 我想旋转我的UIImageView并根据纵横比设置大小。 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. 因此,这两行设置了我的imageView的大小和contentMode。 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. 问题是我的imageView具有无法覆盖的最小大小。 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. 但是我发现,如果将imageView的contentMode属性从UIViewContentModeCenter更改为UIViewContentModeScaleAspectFit,它将很好用,但大小小于我的预期。 Why is that? 这是为什么? :( How can I solve this? Please if you have any help! :(我该如何解决?如果您有任何帮助!

ScaleAspectFit will only scale up the content so that it is all visible and no clipping occurs. ScaleAspectFit将仅按比例放大内容,以便所有内容都可见并且不会发生裁剪。 If you want your content to fill the view and possibly have some clipping, you should use UIViewContentModeScaleAspectFill instead. 如果您希望内容填充视图并可能有一些剪裁,则应改用UIViewContentModeScaleAspectFill

However, if all you want to do is enforce a minimum size for each dimension, you could use the MAX macro instead. 但是,如果您要做的只是为每个尺寸设置一个最小尺寸,则可以改用MAX宏。

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) . 另外,只需检查您的实际意图是使景观尺寸为(width*width/height, width)而不是(height, width)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM