如何旋转UIImage

时间:2013-02-13 16:04:57

标签: objective-c ipad ios6 uiimageview uiimage

我正在开发适用于iPad的iOS应用。有没有办法旋转UIImage90º然后将其添加到UIImageView?我尝试了很多不同的代码,但都没有用......

谢谢!

10 个答案:

答案 0 :(得分:21)

您可以使用

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

或者,如果您真的想要更改图片,可以使用this answer中的代码,它可以正常工作。

答案 1 :(得分: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;
}

可以为orientation参数传递其他可能的值,以实现180度旋转和翻转等。

答案 2 :(得分:9)

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

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

{{1}}

答案 3 :(得分: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;

答案 4 :(得分:5)

以下是@ 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)

答案 5 :(得分:4)

使用Swift,您可以通过执行以下操作来旋转图像:

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

答案 6 :(得分:3)

UIImage *img = [UIImage imageWithName@"aaa.png"];
UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight];

答案 7 :(得分:1)

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

获得上下文后,请使用CGContextRotateCTM

更多信息on this Apple Doc

答案 8 :(得分:1)

感谢杰森克罗克解决了我的问题。两个位置只有一个小的校正,交换高度和宽度,并且没有失真,即

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程序,您可以轻松测试图像是否适用于非苹果世界。

答案 9 :(得分: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可用于任何地方.. 快乐的编码......