首页 文章

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

提问于
浏览
39

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

11 回答

  • 11
    self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
    

    Swift 4:

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

    这是将图像旋转到任何程度的完整代码,只需将其添加到适当的文件,即在.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
    

    这是苹果公司的代码片段SquareCam示例 .

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

    UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
    

    这里 square 是一个 UIImagerotationDegrees 是一个 flote ivar旋转图像度

  • 8

    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)
    
  • 108
    - (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;
    
    }
    
  • 13

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

    Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
    
  • 0

    我在XCode 6.3 Swift 1.2中尝试了这个错误

    self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
    

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

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

    Swift 3版本:

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

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

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

    Swift 4+.

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

    Note: angle is in radian

    .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)
    
            }
            })
        }
    

    Output:

    enter image description here

  • 6

    这是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;
    
    }
    
  • 2

    最好只使用位图 . 在我的情况下使用在reference.image中的UIImage,我在base64String转换了图像 . 没有CGAffineTransformMakeRotation而没有rotateViewBox.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;
    

相关问题