How to rotate an image 90 degrees on iOS?

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?

  • Incorrect rotation of a view controller in iOS 6
  • iOS7 / iOS6 Conditional Rotation Portrait / Landscape for different sections of App
  • Why does this rotate method give the image dead space? OpenCV
  • How can I make a vertical slider in swift?
  • Disable rotation for View Controller in Navigation Controller
  • UICollectionView - resizing cells on device rotate - Swift
  • 8 Solutions Collect From Internet About “How to rotate an image 90 degrees on iOS?”

    self.imageview.transform = CGAffineTransformMakeRotation(M_PI_2);
    

    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

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

    Simply add this code to the image.

    Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
    

    I got an error trying this in 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 3.1 uses .pi instead

    eg:

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

    Swift 3 version:

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

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