如何将UIImage和图像数据旋转90度

我有一个包含图像的UIImageView。 当时用户可以点击将UIImageView中的图像保存到磁盘。

我想这样做,使用户可以点击旋转UIImageView,也旋转视图内的UImage,使图像保存时,保持新的旋转。

在那一刻,我有

- (IBAction)rotateImage:(id)sender { float degrees = 90; //the value in degrees self.imagePreview.transform = CGAffineTransformMakeRotation(degrees * M_PI/180); } 

有人可以指出我保持当前的轮换正确的方向。

谢谢

我在旧的应用程序中有这样的代码。 但是这个代码可以简化,因为你不需要在非90度的angular度上旋转它。

 - (UIImage *)rotateImage:(UIImage *)image onDegrees:(float)degrees { CGFloat rads = M_PI * degrees / 180; float newSide = MAX([image size].width, [image size].height); CGSize size = CGSizeMake(newSide, newSide); UIGraphicsBeginImageContext(size); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(ctx, newSide/2, newSide/2); CGContextRotateCTM(ctx, rads); CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(-[image size].width/2,-[image size].height/2,size.width, size.height),image.CGImage); UIImage *i = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return i; } 

这将以任何给定的弧度旋转图像。

注意这也适用于2x和3x视网膜

 - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees { CGFloat radians = DegreesToRadians(degrees); UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.size.width, self.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(radians); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]); CGContextRef bitmap = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2); CGContextRotateCTM(bitmap, radians); 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; } 

UIButton突出显示的状态并没有采取正常插入转动图像的正确方向。 所以我不得不重新绘制像@RyanG所示的图像。 这是Swift 2.2的代码:

 extension UIImage { /// Rotate an image by any given radians. /// Works for 2x and 3x retina as well. func imageRotatedByDegrees(degrees: Double) -> UIImage { let radians = CGFloat(degrees * (M_PI / 180.0)) let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: self.size)) let t: CGAffineTransform = CGAffineTransformMakeRotation(radians) rotatedViewBox.transform = t let rotatedSize = rotatedViewBox.frame.size UIGraphicsBeginImageContextWithOptions(rotatedSize, false, self.scale) let bitmap = UIGraphicsGetCurrentContext() CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2) CGContextRotateCTM(bitmap, radians) CGContextScaleCTM(bitmap, 1.0, -1.0) CGContextDrawImage(bitmap, CGRect(origin: CGPoint(x: -self.size.width / 2, y: -self.size.height / 2), size: self.size), self.CGImage) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } } 

self.imageview.transform = CGAffineTransformMakeRotation(M_PI/2);