IOS - 如何旋转图像到一定程度

IOS - How to rotate image at some degree

我想将图像旋转一定角度,如下图所示。

我正在尝试

CGAffineTransform t = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
imageView.transform = t;

我没有得到这样的输出。即使我不熟悉这个。

在应用其他变换之前通过变换重置...

imageView.transform = CGAffineTransformIdentity  
// i hope your angle is proper calculate
CGAffineTransform t = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
imageView.transform = t;

希望对您有所帮助

这是我用来将图像旋转 30 度的代码

 UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 400)];
 imgView.image = [UIImage imageNamed:@"maxresdefault.jpg"];
 [self.view addSubview:imgView];
 float degrees = 30.0f;
 float radians = (degrees* M_PI/180);
 imgView.layer.transform = CATransform3DMakeRotation(radians, 0, 0, 1.0);

转换前的模拟器截图

转换后的模拟器截图

希望这对您有所帮助。

您可以像下面在 swift

中更改 layer.transform
imageView?.layer.transform = CATransform3DMakeAffineTransform(CGAffineTransform(rotationAngle: CGFloat(M_PI_2)))

对于Objective C:

imageView.layer.transform = CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation(M_PI_2));