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

问题描述 投票:0回答:15

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

iphone objective-c ios rotation
15个回答
128
投票
self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);

斯威夫特4+:

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

30
投票

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

对于.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];

这里 square 是 1

UIImage
rotationDegrees 是 1
flote
ivar,用于将图像旋转度


23
投票

Swift 3 和 Swift 4 使用 .pi 代替

例如:

//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)

14
投票
- (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;

}

8
投票

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

Image.transform=CGAffineTransformMakeRotation(M_PI / 2);

6
投票

我在 XCode 6.3 Swift 1.2 中尝试此操作时遇到错误

self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);

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

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

Swift 4 编辑

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

3
投票

Swift 3 版本:

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

3
投票

斯威夫特 4+。

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

注意:角度的单位是弧度

.pi = 180 度

.pi/2 = 90 度

如果你想添加动画

  @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)

        }
        })
    }

输出:


2
投票

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

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

2
投票
 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

}

功能使用

imageRotatedByDegrees(oldImage: yourImage, deg: degree) 

1
投票

Image
级别、在 Swift 5 中进行操作:

extension UIImage{
    
    
    func imageRotated(by radian: CGFloat) -> UIImage{
        
        let rotatedSize = CGRect(origin: .zero, size: size)
            .applying(CGAffineTransform(rotationAngle: radian))
            .integral.size
        UIGraphicsBeginImageContext(rotatedSize)
        if let context = UIGraphicsGetCurrentContext() {
            let origin = CGPoint(x: rotatedSize.width / 2.0,
                                 y: rotatedSize.height / 2.0)
            context.translateBy(x: origin.x, y: origin.y)
            context.rotate(by: radian)
            draw(in: CGRect(x: -origin.y, y: -origin.x,
                            width: size.width, height: size.height))
            let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return rotatedImage ?? self
        }

        return self

    }
}

0
投票

这是 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;

}

0
投票

最好只使用位图。在我的例子中,使用reference.image中的UIImage,并在base64String处转换了图像。没有CGAffineTransformMakeRotation并且没有rotatedViewBox.transform

var rotatedSize = reference.image.size;
                //Create bitmap context
                UIGraphicsBeginImageContext(rotatedSize);
                var bitmap = UIGraphicsGetCurrentContext();
                //move origin to the middle of the image for rotation
                CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
                //rotate image context
                CGContextRotateCTM(bitmap, 1.5708);
                CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
                var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
                var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
                UIGraphicsEndImageContext();
                return base64Image;

0
投票

如果您要保存图像,那么您需要旋转图片上下文而不是视图。我已经使用了上面提供的示例,但它们在图像居中时效果不佳。所以我进行了如下增强和修复:

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

    UIGraphicsBeginImageContext(CGSize(width: self.size.height, height: self.size.width))

    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: self.size.width / 2, y: self.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: -self.size.height / 2, y: -self.size.width / 2)

    bitmap.draw(self.cgImage!, in: CGRect(origin: origin, size: CGSize(width: self.size.width, height: self.size.height)))

    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!

    UIGraphicsEndImageContext()

    return newImage
}

0
投票

如果您只想将图像在任意方向旋转 90 度,这可能会有所帮助:

let yourImage = ...
let rotatedImage = UIImage(cgImage: yourImage.cgImage!, scale: 1.0, orientation: .left)

不要使用

.left
,而是使用
.right
表示相反的 90 度方向。

© www.soinside.com 2019 - 2024. All rights reserved.