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

我是使用转换的新手。 并且仍然担心他们的工作方式。

我正在尝试做的是以给定的角度旋转我的UIImageView。 但旋转后,它会改变图像的大小,变小。 我也正在为ImageView进行缩放,因此它不会颠倒。 当分配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]; 

非常感谢!

好的,我保证我会调查它,所以这是我的答案:

我创建了一个应该与你的相似的场景,代码如下:

 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]); 

此代码假定您使用的是ARC。 如果没有添加

 [imageView release]; 

最后。

使用此代码,日志如下所示:

 [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}} 

正如您所看到的那样,界限总是保持不变。 由于旋转而实际变化的是框架,因为旋转了30°C的图像当然比没有旋转的图像宽。 并且由于中心点已设置为视图的实际中心,因此框架的原点也会发生变化(被推到左侧和顶部)。 请注意,图像本身的大小不会改变 。 我没有使用比例变换,因为结果可以在没有缩放的情况下实现。

但为了使它更清晰,有些图片适合你(0°,30°90°旋转): UIImageView的0°,30°和90°旋转

它们看起来非常相似,对吧? 我画了实际的帧来清楚界限和帧之间的区别是什么。 下一个真的很清楚。 我覆盖了所有图像,通过UIImageView旋转的负度旋转它们,得到以下结果: 重叠的UIImageViews显示大小不会改变

所以你看到如何旋转图像非常简单。 现在问题你实际上希望框架保持不变。 如果您希望最终帧具有原始帧的大小(在此示例中宽度为200,高度为250),则必须缩放生成的帧。 但这当然会导致您不想要的图像缩放。 我实际上认为一个更大的框架对你来说不是问题 – 你只需要知道你必须考虑到因为旋转而考虑它。

简而言之:不可能有一个旋转后具有相同帧的UIImageView。 任何UIView都无法做到这一点。 想想一个矩形。 如果你旋转它,旋转后它不会是一个矩形,是吗?

当然你可以将你的UIImageView放在另一个UIView中,它将有一个宽度为200且高度为250的非旋转框架,但这只是表面的,因为它不会真正改变旋转的矩形有一个不同的宽度和高度比原来。

我希望这有帮助。 🙂

不要设置UIImageView从UIViewinheritance的contentMode,并根据您选择的UIViewContentMode根据缩放,转换,设备旋转导致帧中的更改。

此外,如果您只是想旋转,您可以使用以下方法更改框架: –

 [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]; } 

如果你不想动画只是改变框架

尝试使用此: –

 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"];