旋转UIImageView大小后为什么会改变? [英] Why after rotating UIImageView size is getting changed?

查看:142
本文介绍了旋转UIImageView大小后为什么会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new in using transformations. And still confusted how they are working.

我正在尝试做的是以给定的角度旋转我的UIImageView。但旋转后,它会改变图像的大小,变小。我也正在为ImageView进行缩放,因此它不会颠倒。如何在分配ImageView时旋转并保持CGRectMake中给出的大小?

What I'm trying to do, is to rotate my UIImageView with given angle. 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.How to rotate and keep the size, that was given in CGRectMake, when ImageView was allocated ?

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

非常感谢!

推荐答案

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

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

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

This code assumes that you are using ARC. If not add

[imageView release];   

最后。

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

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

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

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. 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.

但为了让它更清晰,这里有一些图片(0°,30°)旋转90°):

But to make it clearer here are some pictures for you (0°, 30° 90° rotation):

它们看起来非常相似,对吧?我画了实际的帧来清楚界限和帧之间的区别是什么。下一个真的很清楚。我覆盖了所有图像,按UIFmageView旋转的负度旋转它们,得到以下结果:

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:

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

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. 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.

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

In short: it is not possible to have an UIImageView which will have the same frame after rotation. This isn't possible for any UIView. Just think of a rectangle. If you rotate it, it won't be a rectangle after the rotation, will it?

当然你可以把你的UIImageView放在另一个UIViewView里面旋转的框架宽度为200,高度为250,但这只是表面的,因为它不会真正改变旋转的矩形具有与原始宽度和高度不同的宽度和高度的事实。

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.

我希望这会有所帮助。 :)

I hope this helps. :)

这篇关于旋转UIImageView大小后为什么会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆