如何在iOS上旋转90度的图像?

我想要做的是从我的相机拍摄快照,将其发送到服务器,然后服务器发送给我一个viewController的图像。 如果图像处于肖像模式,则图像在屏幕上显示效果良好,但是如果图像是在横向模式下拍摄的,则图像在屏幕上显示为拉伸(因为它会尝试以纵向模式显示!)。 我不知道如何解决这个问题,但我想一个解决scheme是首先检查图像是在纵向/横向模式,然后如果是在横向模式旋转90度,然后再显示在屏幕上。 那我怎么能这样做?

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 

这是图像旋转到任何程度的完整代码,只需将其添加到适当的文件,即在.m下面你想要使用的image processing

.m

 @interface UIImage (RotationMethods) - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; @end @implementation UIImage (RotationMethods) static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees { // calculate the size of the rotated view's containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // // Rotate the image context CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end 

这是苹果的SquareCam例子的代码片段。

要调用上面的方法只需使用下面的代码

 UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees]; 

这里的广场是一个UIImagerotationDegrees是一个flote ivar旋转度的图像

 - (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees { UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; [rotatedViewBox release]; UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(bitmap, rotatedSize.width, rotatedSize.height); CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

只需将此代码添加到图像。

 Image.transform=CGAffineTransformMakeRotation(M_PI / 2); 

我在Xcode 6.3 Swift 1.2中遇到了一个错误

 self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2); 

你应该试试这个强制types转换修复:

 self.imageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)); 

Swift 3和Swift 4改为使用.pi

例如:

 //rotate 270 degrees myImageView.transform = CGAffineTransform(rotationAngle: (.pi * 1.5)) //rotate 90 degrees myImageView.transform = CGAffineTransform(rotationAngle: (.pi / 2)) 

Swift 3版本:

 imageView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2)) 

Swift 4 | Xcode 9语法 (请注意,此代码顺时针旋转UIImageView 90度,而不是UIImage):

 myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2)) 

这里是iOSDev答案的Swift 2.2版本(在UIImage扩展中):

 func degreesToRadians(degrees: CGFloat) -> CGFloat { return degrees * CGFloat(M_PI) / 180 } func imageRotatedByDegrees(degrees: CGFloat) -> UIImage { // calculate the size of the rotated view's containing box for our drawing space let rotatedViewBox = UIView(frame: CGRectMake(0,0,self.size.width, self.size.height)) let t = CGAffineTransformMakeRotation(self.degreesToRadians(degrees)) rotatedViewBox.transform = t let rotatedSize = rotatedViewBox.frame.size // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize) let bitmap = UIGraphicsGetCurrentContext() // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), self.CGImage); let newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }