简体   繁体   English

旋转UIImageView大小后为什么会改变?

[英]Why after rotating UIImageView size is getting changed?

I'm new in using transformations. 我是使用转换的新手。 And still confusted how they are working. 并且仍然担心他们的工作方式。

What I'm trying to do, is to rotate my UIImageView with given angle. 我正在尝试做的是以给定的角度旋转我的UIImageView。 But after rotating, it's changing the size of image, getting smaller. 但旋转后,它会改变图像的大小,变小。 I'm also doing scaling for ImageView so it won't be upside down. 我也正在为ImageView进行缩放,因此它不会颠倒。 How to rotate and keep the size, that was given in CGRectMake, when ImageView was allocated ? 当分配ImageView时,如何旋转并保持CGRectMake中给出的大小?

    UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,y,width,height)];        

    myImageView.contentMode = UIViewContentModeScaleAspectFit;

    [myImageView setImage:[UIImage imageNamed:@"image.png"]];

    myImageView.layer.anchorPoint = CGPointMake(0.5,0.5);

    CGAffineTransform newTransform;

    myImageView.transform = CGAffineTransformMakeScale(1,-1);            

    newTransform = CGAffineTransformRotate(newTransform, 30*M_PI/180);

    [self.window addSubview:myImageView];

Thanks a lot! 非常感谢!

Ok I promised I'd look into it, so here's my answer: 好的,我保证我会调查它,所以这是我的答案:

I create a scene which should be somewhat equivalent to yours, code as follows: 我创建了一个应该与你的相似的场景,代码如下:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-100,
                                                                       self.view.bounds.size.height/2-125,
                                                                       200,
                                                                       250)];

imageView.image = [UIImage imageNamed:@"testimage.jpg"];
imageView.contentMode = UIViewContentModeScaleAspectFill;

/*
 * I added clipsToBounds, because my test image didn't have a size of 200x250px
 */
imageView.clipsToBounds = YES;

[self.view addSubview:imageView];

NSLog(@"frame: %@",[NSValue valueWithCGRect:imageView.frame]);
NSLog(@"bounds: %@",[NSValue valueWithCGRect:imageView.bounds]);    

imageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
imageView.transform = CGAffineTransformMakeRotation(30*M_PI/180);

NSLog(@"frame after rotation: %@",[NSValue valueWithCGRect:imageView.frame]);
NSLog(@"bounds after rotation: %@",[NSValue valueWithCGRect:imageView.bounds]); 

This code assumes that you are using ARC. 此代码假定您使用的是ARC。 If not add 如果没有添加

[imageView release];   

at the end. 在末尾。

Using this code the logs look like this: 使用此代码,日志如下所示:

[16221:207] frame: NSRect: {{60, 105}, {200, 250}}
[16221:207] bounds: NSRect: {{0, 0}, {200, 250}}
[16221:207] frame after rotation: NSRect: {{10.897461, 71.746826}, {298.20508, 316.50635}}
[16221:207] bounds after rotation: NSRect: {{0, 0}, {200, 250}}    

As you can see the bounds always stay the same. 正如您所看到的那样,界限始终保持不变。 What actually changes due to the rotation is the frame, because an image which has been rotated by 30°C is of course wider than if it handn't been rotated. 实际上由于旋转而改变的是框架,因为旋转了30°C的图像当然比没有旋转的图像宽。 And since the center point has been set to the actual center of the view the origin of the frame also changes (being pushed to the left and the top). 并且由于中心点已设置为视图的实际中心,因此框架的原点也会发生变化(被推到左侧和顶部)。 Notice that the size of the image itself does not change . 请注意,图像本身的大小不会改变 I didn't use the scale transformation, since the result can be achieved without scaling. 我没有使用比例变换,因为结果可以在没有缩放的情况下实现。

But to make it clearer here are some pictures for you (0°, 30° 90° rotation): 但为了使它更清晰,有些图片适合你(0°,30°90°旋转): UIImageView的0°,30°和90°旋转

They already look pretty similar, right? 它们看起来非常相似,对吧? I drew the actual frames to make it clear what's the difference between bounds and frame is. 我绘制了实际的帧以清楚地说明边界和帧之间的区别是什么。 The next one really makes it clear. 下一个真的很清楚。 I overlayed all images, rotating them by the negative degrees with which the UIImageView was rotated, giving the following result: 我覆盖了所有图像,通过UIImageView旋转的负度旋转它们,得到以下结果: 重叠的UIImageViews显示大小不会改变

So you see it's pretty straight forward how to rotate images. 所以你看到如何旋转图像非常简单。 Now to your problem that you actually want the frame to stay the same. 现在问题你实际上希望框架保持不变。 If you want the final frame to have the size of your original frame (in this example with a width of 200 and a height of 250) then you will have to scale the resulting frame. 如果您希望最终帧具有原始帧的大小(在此示例中宽度为200,高度为250),则必须缩放生成的帧。 But this will of course result in scaling of the image, which you do not want. 但这当然会导致图像缩放,这是您不想要的。 I actually think a larger frame will not be a problem for you - you just need to know that you have to take it into account because of the rotation. 我实际上认为一个更大的框架对你来说不是问题 - 你只需要知道你必须考虑因为旋转而考虑它。

In short: it is not possible to have an UIImageView which will have the same frame after rotation. 简而言之:不可能有一个旋转后具有相同帧的UIImageView。 This isn't possible for any UIView. 任何UIView都无法做到这一点。 Just think of a rectangle. 想想一个矩形。 If you rotate it, it won't be a rectangle after the rotation, will it? 如果你旋转它,旋转后它不会是一个矩形,是吗?

Of course you could put your UIImageView inside another UIView which will have a non-rotated frame with a width of 200 and a height of 250 but that would just be superficial, since it won't really change the fact that a rotated rectangle has a different width and height than the original. 当然你可以将你的UIImageView放在另一个UIView中,它将有一个宽度为200且高度为250的非旋转框架,但这只是表面的,因为它不会真正改变旋转的矩形有一个不同的宽度和高度比原来的。

I hope this helps. 我希望这有帮助。 :) :)

Do not set the contentMode which UIImageView inherits from UIView and leads to the changes in frame according to scaling,transformation,device rotation in accordance to the UIViewContentMode you select. 不要设置UIImageView从UIView继承的contentMode,并根据您选择的UIViewContentMode根据缩放,转换,设备旋转导致帧中的更改。

Also if you just want to rotate you can just change the frame using :- 此外,如果您只想旋转,您可以使用以下方法更改框架: -

[UIView beginAnimations:@"Rotate" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    CGRect frame=yourView.frame;
    frame.origin.y+=distance you want to rotate;
    yourview.frame=frame;
    [UIView commitAnimations];
}

if you dont want the animation just change the frame 如果你不想动画只是改变框架

Try Using This :- 尝试使用此: -

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 0.5f;            
animation.repeatCount = HUGE_VALF;     // HUGE_VALF is defined in math.h so import it
[self.reloadButton.imageView.layer addAnimation:animation forKey:@"rotation"];

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

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