How to rotate an image 90 degrees on iOS?

后端 未结 14 1856
野的像风
野的像风 2020-11-29 00:55

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 t

相关标签:
14条回答
  • 2020-11-29 01:44

    Simply add this code to the image.

    Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
    
    0 讨论(0)
  • 2020-11-29 01:45

    If you are looking to save image then you need to rotate picture context not the view. I have used provided examples above but they did not work well when centering the image. So I have enhanced and fixed as follows :

    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 讨论(0)
  • 2020-11-29 01:48

    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

    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 SquareCam example.

    To call the above method just use the below code

    UIImage *rotatedSquareImage = [square imageRotatedByDegrees:rotationDegrees];
    

    Here the square is one UIImage and rotationDegrees is one flote ivar to rotate the image that degrees

    0 讨论(0)
  • 2020-11-29 01:51
    self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
    

    Swift 4+:

    self.imageview.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2))
    
    0 讨论(0)
  • 2020-11-29 01:52

    Here is the Swift 2.2 version of The iOSDev answer (in UIImage extension):

    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 讨论(0)
  • 2020-11-29 01:53
     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) 
    
    0 讨论(0)
提交回复
热议问题