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

Use*_*234 6 cocoa-touch objective-c ios ios5

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

我正在尝试做的是以给定的角度旋转我的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];
Run Code Online (Sandbox Code Playgroud)

非常感谢!

Joh*_*mpe 17

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

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

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]); 
Run Code Online (Sandbox Code Playgroud)

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

[imageView release];   
Run Code Online (Sandbox Code Playgroud)

在末尾.

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

[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}}    
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

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

我希望这有帮助.:)

  • 这些有用的[UIKit函数]有很多负载(http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html) (2认同)