How to Rotate a UIImage 90 degrees?
What about something like:
static inline double radians (double degrees) {return degrees * M_PI/180;}
UIImage* rotate(UIImage* src, UIImageOrientation orientation)
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, radians(90));
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, radians(-90));
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, radians(90));
}
[src drawAtPoint:CGPointMake(0, 0)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
How to rotate UIImage
You may rotate UIImageView
itself with:
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
iv.transform = CGAffineTransformMakeRotation(M_PI_2);
Or if you really want to change image, you may use code from this answer, it works.
Rotate UIImage in swift
This works for me:
works Fine :)
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {
//Calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox: UIView = UIView(frame: CGRectMake(0, 0, oldImage.size.width, oldImage.size.height))
let t: CGAffineTransform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI / 180))
rotatedViewBox.transform = t
let rotatedSize: CGSize = rotatedViewBox.frame.size
//Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap: CGContextRef = 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, (degrees * CGFloat(M_PI / 180)))
//Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0)
CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), oldImage.CGImage)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
Swift 3:
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {
//Calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: oldImage.size.width, height: oldImage.size.height))
let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi / 180)
rotatedViewBox.transform = t
let rotatedSize: CGSize = rotatedViewBox.frame.size
//Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
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: rotatedSize.width / 2, y: rotatedSize.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat.pi / 180))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
bitmap.draw(oldImage.cgImage!, in: CGRect(x: -oldImage.size.width / 2, y: -oldImage.size.height / 2, width: oldImage.size.width, height: oldImage.size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
How to rotate image in Swift?
This is an extension of UIImage
that targets Swift 4.0 and can rotate just the image without the need for a UIImageView
. Tested successfully that the image was rotated, and not just had its exif data changed.
import UIKit
extension UIImage {
func rotate(radians: CGFloat) -> UIImage {
let rotatedSize = CGRect(origin: .zero, size: size)
.applying(CGAffineTransform(rotationAngle: CGFloat(radians)))
.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: radians)
draw(in: CGRect(x: -origin.y, y: -origin.x,
width: size.width, height: size.height))
let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return rotatedImage ?? self
}
return self
}
}
To perform a 180 degree rotation, you can call it like this:
let rotatedImage = image.rotate(radians: .pi)
If for whatever reason it fails to rotate, the original image will then be returned.
Rotate a UIImage
UIImage *yourImageHere;
switch (imageView.image.imageOrientation)
{
case UIImageOrientationUp:
newImage=[UIImage imageWithCGImage:[imageView.image CGImage] scale:1.0f orientation:UIImageOrientationLeft];
imageView.contentMode=UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationLeft:
newImage=[UIImage imageWithCGImage:[imageView.image CGImage] scale:1.0f orientation:UIImageOrientationDown];
imageView.contentMode=UIViewContentModeScaleAspectFill;
break;
case UIImageOrientationDown:
newImage=[UIImage imageWithCGImage:[imageView.image CGImage] scale:1.0f orientation:UIImageOrientationRight];
imageView.contentMode=UIViewContentModeScaleAspectFit;
break;
case UIImageOrientationRight:
newImage=[UIImage imageWithCGImage:[imageView.image CGImage] scale:1.0f orientation:UIImageOrientationUp];
imageView.contentMode=UIViewContentModeScaleAspectFill;
break;
default:
break;
}
imageView.image=newImage;
May this helps you
Related Topics
Ios: How to Make a Shadow for Uiview on 4 Side (Top,Right,Bottom and Left)
Swift - Integer Conversion to Hours/Minutes/Seconds
Determine Device (Iphone, Ipod Touch) With Ios
Set the Maximum Character Length of a Uitextfield
How to Find Topmost View Controller on Ios
How to Easily Resize/Optimize an Image Size With Ios
Iphone: Detecting User Inactivity/Idle Time Since Last Screen Touch
How to Create a Multiline Textfield in Swiftui
Play Youtube Videos With Mpmovieplayercontroller Instead of Uiwebview
Why Does Viewwillappear Not Get Called When an App Comes Back from the Background
Wkwebview Not Loading Webpage - Renders Blank Screen in Swift
How to Remove Constraints Programmatically That Is Added from Storyboard
How to Determine the Current Iphone/Device Model
How to Exit Iphone Application
Swift: Extra Argument 'Error' in Call
Importing Commoncrypto in a Swift Framework
Waiting Until Two Async Blocks Are Executed Before Starting Another Block