2013-02-13 96 views
15

我正在为iPad开发iOS应用程序。有没有办法旋转UIImage90º然后将其添加到UIImageView?我试了很多不同的代码,但都没有工作...如何旋转UIImage

谢谢!

回答

18

您可以旋转UIImageView本身:

UIImageView *iv = [[UIImageView alloc] initWithImage:image]; 
iv.transform = CGAffineTransformMakeRotation(M_PI_2); 

或者,如果你真的想改变形象,你可以使用代码this answer,它的工作原理。

+0

我做喜欢说,但是我有一个问题...我从旋转图像左上角,我怎么能设置它从UIImage的中心旋转图像? – 2013-02-13 16:41:39

+0

你可以玩'iv.layer.anchorPoint',但它的默认值是一个图层的中心,所以我不确定它为什么会从左上角旋转图像。 – kovpas 2013-02-13 16:47:17

+0

不,但我使用了您提供给我的链接中的代码。所以这是CGContextRotateCTM – 2013-02-13 16:57:11

1

这样做的另一种方法是使用Core Graphics再次渲染UIImage。

一旦你有了上下文,使用CGContextRotateCTM

更多信息on this Apple Doc

7

还有imageWithCIImage:scale:orientation如果你想旋转UIImage不是UIImageView

与这些方向之一:

typedef enum { 
    UIImageOrientationUp, 
    UIImageOrientationDown, // 180 deg rotation 
    UIImageOrientationLeft, // 90 deg CW 
    UIImageOrientationRight, // 90 deg CCW 
    UIImageOrientationUpMirrored, // vertical flip 
    UIImageOrientationDownMirrored, // horizontal flip 
    UIImageOrientationLeftMirrored, // 90 deg CW then perform horizontal flip 
    UIImageOrientationRightMirrored, // 90 deg CCW then perform vertical flip 
} UIImageOrientation; 
17

要旋转的像素可以使用以下。这创建了一个具有旋转元数据的中间UIImage,并将其呈现为宽高比尺寸调换的图像上下文。将得到的图像具有旋转(即基本CGImage)的像素

- (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise 
{ 
    CGSize size = sourceImage.size; 
    UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width)); 
    [[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

有可以传递为取向参数,实现180度旋转的其它可能的值和翻转等

+0

如果原始图像是'UIImageOrientationRight',并且您在此处设置了'UIImageOrientationLeft',那么生成的输出将被压扁以适应新的尺寸。 – ray 2016-04-29 17:26:36

3
UIImage *img = [UIImage [email protected]"aaa.png"]; 
UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight]; 
4

随着斯威夫特,你可以通过做一个图像旋转:

var image: UIImage = UIImage(named: "headerBack.png") 
var imageRotated: UIImage = UIImage(CGImage: image.CGImage, scale:1, orientation: UIImageOrientation.UpMirrored) 
8

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

注意这个作品2倍和3倍视网膜以及

- (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; 
} 
+0

这很好,但是有没有办法做到这一点,而不必创建一个UIView?我的理解是,UIView是一个相当重量级的对象。谢谢! – Mario 2016-12-29 21:55:46

+0

我发现一个CALayer可以用来代替UIView。创建图层后,必须设置框架属性。您还必须将CATransform3DMakeAffineTransform函数应用于CAAffineTransform对象(“t​​”)以设置图层上的transform属性。其他一切按原样运作。我的理解是CALayer是一个更轻量级的对象。 – Mario 2016-12-30 16:24:13

4

这里是@迅速版RyanG的Objective C代码的一个扩展的UIImage:

extension UIImage { 

    func rotate(byDegrees degree: Double) -> UIImage { 
     let radians = CGFloat(degree*M_PI)/180.0 as CGFloat 
     let rotatedViewBox = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)) 
     let t = CGAffineTransform(rotationAngle: radians) 
     rotatedViewBox.transform = t 
     let rotatedSize = rotatedViewBox.frame.size 
     let scale = UIScreen.main.scale 
     UIGraphicsBeginImageContextWithOptions(rotatedSize, false, scale) 
     let bitmap = UIGraphicsGetCurrentContext() 
     CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

     bitmap!.rotate(by: radians); 

     bitmap!.scaleBy(x: 1.0, y: -1.0); 
     CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2 , self.size.width, self.size.height), self.CGImage); 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() 

     return newImage 
    } 

} 

用法image.rotate(degree)

1

谢谢Jason Crocker解决了我的问题。只有一个小小的修正,在这两个位置互换的高度和宽度,并没有出现失真,即

UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height)); 
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.width,size.height)]; 

我的问题不能由CGContextRotateCTM来解决,我不知道为什么。我的问题是,我正在将我的图像传输到服务器,并一直显示90度。通过将图像复制到您在Mac上运行的MS Office程序,您可以轻松测试图像是否在非苹果世界中运行。

1

这就是我所做的,当我想改变图像的方向(顺时针旋转90度)。

//Checking for the orientation ie, image taken from camera is in portrait or not. 
if(yourImage.imageOrientation==3) 
{ 
    //Image is in portrait mode. 
    yourImage=[self imageToRotate:yourImage RotatedByDegrees:90.0]; 
} 

- (UIImage *)image:(UIImage *)imageToRotate RotatedByDegrees:(CGFloat)degrees 
{ 
    CGFloat radians = degrees * (M_PI/180.0); 

    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.height, image.size.width)]; 
    CGAffineTransform t = CGAffineTransformMakeRotation(radians); 
    rotatedViewBox.transform = t; 
    CGSize rotatedSize = rotatedViewBox.frame.size; 

    UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]); 
    CGContextRef bitmap = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(bitmap, rotatedSize.height/2, rotatedSize.width/2); 

    CGContextRotateCTM(bitmap, radians); 

    CGContextScaleCTM(bitmap, 1.0, -1.0); 
    CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2 , image.size.height, image.size.width), image.CGImage); 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

旋转后的图像可以是大小> = 15MB(从我的经验)的。所以你应该压缩它并使用它。否则,您可能遇到导致内存压力的崩溃。下面给出了用于压缩的代码。

NSData *imageData = UIImageJPEGRepresentation(yourImage, 1); 
//1 - it represents the quality of the image. 
NSLog(@"Size of Image(bytes):%d",[imageData length]); 
//Here I used a loop because my requirement was, the image size should be <= 4MB. 
//So put an iteration for more than 1 time upto when the image size is gets <= 4MB. 
for(int loop=0;loop<100;loop++) 
{ 
    if([imageData length]>=4194304) //4194304 = 4MB in bytes. 
    { 
     imageData=UIImageJPEGRepresentation(yourImage, 0.3); 
     yourImage=[[UIImage alloc]initWithData:imageData]; 
    } 
    else 
    { 
     NSLog(@"%d time(s) compressed.",loop); 
     break; 
    } 
} 

现在你yourImage可用于任何地方.. 快乐编码...