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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11667565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:27:11  来源:igfitidea点击:

How to rotate an image 90 degrees on iOS?

iphoneobjective-ciosrotation

提问by user1511244

What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode the image appears well on screen , however if the image was taken in landscape mode the image appears streched on the screen(as it tries to appear on portrait mode!). I dont know how to fix this but i guess one solution is first to check if the image is in portrait/landscape mode and then if it is on landscape mode rotate it by 90degrees before showing it on screen. So how could i do that?

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

回答by mandeep-dhiman

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);

Swift 4+:

斯威夫特 4+:

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

回答by The iOSDev

This is the complete code for rotation of image to any degree just add it to appropriate file ie in .m as below where you want to use the image processing

这是将图像旋转到任何程度的完整代码,只需将其添加到适当的文件中,即在 .m 中,如下所示,您要在其中使用图像处理

for .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

This is the code snippet form apple's SquareCamexample.

这是 Apple 的SquareCam示例中的代码片段。

To call the above method just use the below code

要调用上述方法只需使用以下代码

UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];

Here the squareis one UIImageand rotationDegreesis one floteivar to rotate the image that degrees

这里的正方形是一个,UIImagerotationDegrees是一个floteivar 来旋转图像的度数

回答by Jesus Rodriguez

Swift 3 and Swift 4 use .pi instead

Swift 3 和 Swift 4 使用 .pi 代替

eg:

例如:

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

//rotate 180 degrees
myImageView.transform = CGAffineTransform(rotationAngle: .pi)

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

回答by janusfidel

- (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;

}

回答by IronManGill

Simply add this code to the image.

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

Image.transform=CGAffineTransformMakeRotation(M_PI / 2);

回答by mattyU

I got an error trying this in XCode 6.3 Swift 1.2

我在 XCode 6.3 Swift 1.2 中尝试此操作时出错

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);

You should try this to force type cast fix:

你应该试试这个来强制类型转换修复:

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

Swift 4 Edit

斯威夫特 4 编辑

imageview.transform = CGAffineTransform(rotationAngle: .pi)

回答by Hyman

Swift 4+.

斯威夫特 4+。

arrowImageView.transform = CGAffineTransform(rotationAngle: .pi/2)

Note: angle is in radian

注:角度为弧度

.pi = 180 degree

.pi/2 = 90 degree

.pi = 180 度

.pi/2 = 90 度

If you want to add with animation

如果你想添加动画

  @IBAction func applyTransForm(sender: UIButton) {
        sender.isSelected = !sender.isSelected
        UIView.animate(withDuration: 1, animations: {
        if sender.isSelected {
            self.arrowImageView.transform = CGAffineTransform(rotationAngle: .pi)
        } else {
            self.arrowImageView.transform = CGAffineTransform(rotationAngle: 0)

        }
        })
    }

Output:

输出:

enter image description here

在此处输入图片说明

回答by Dhruv Khatri

Swift 3 version:

斯威夫特 3 版本:

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

回答by Frank Dev

Swift 4 | Xcode 9 syntax(please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):

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

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

回答by urvashi bhagat

 func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {

    let size = oldImage.size
    UIGraphicsBeginImageContext(size)

    let bitmap: CGContext = UIGraphicsGetCurrentContext()!
    //Move the origin to the middle of the image so we will rotate and scale around the center.
    bitmap.translateBy(x: size.width / 2, y: size.height / 2)
    //Rotate the image context
    bitmap.rotate(by: (degrees * CGFloat(Double.pi / 180)))
    //Now, draw the rotated/scaled image into the context
    bitmap.scaleBy(x: 1.0, y: -1.0)

    let origin = CGPoint(x: -size.width / 2, y: -size.width / 2)

    bitmap.draw(oldImage.cgImage!, in: CGRect(origin: origin, size: size))

    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return newImage

}

Use of function

函数的使用

imageRotatedByDegrees(oldImage: yourImage, deg: degree)